Почему мой игровой объект удаляется, когда я кладу его на тарелку. Кухонный объектC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Гость
 Почему мой игровой объект удаляется, когда я кладу его на тарелку. Кухонный объект

Сообщение Гость »

Мои игровые объекты продолжают удаляться после размещения на тарелке «Кухонный объект», а не что-либо еще, и это, вероятно, связано с тем, что я внедрил в свой код новую вещь, позволяющую определять, какой «Кухонный объект» находится на тарелке.
/>

Код: Выделить всё

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlateCompleteVisual : MonoBehaviour
{

[Serializable]
public struct KitchenObjectSO_GameObject
{
public KitchenObjectSO kitchenObjectSO;
public GameObject gameObject;
}

[SerializeField] private PlateKitchenObject plateKitchenObject;
public List kitchenObjectSOGameObjectList;

private void Start()
{

}

}

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using static KitchenObjectType;
using static PlateCompleteVisual;

public class KitchenObject : MonoBehaviour
{

public enum ObjectType
{
Bread,
Lettuce,
Cheese,
Tomato,
// Add more types as needed
}

public static void IdentifyKitchenObject(ObjectType objectType)
{
switch (objectType)
{
case ObjectType.Bread:
Debug.Log("Found bread!");
// Handle bread-specific logic
break;
case ObjectType.Lettuce:
Debug.Log("Found lettuce!");
// Handle lettuce-specific logic
break;
case ObjectType.Cheese:
Debug.Log("Found cheese!");
// Handle cheese-specific logic
break;
case ObjectType.Tomato:
Debug.Log("Found tomato!");
// Handle tomato-specific logic
break;
// Add cases for other types as needed
default:
Debug.Log("Unknown kitchen object type!");
break;
}
}

[SerializeField] KitchenObjectSO kitchenObjectSO;
private IKitchenObjectParent kitchenObjectParent;

public KitchenObjectSO GetKitchenObjectSO()
{
return kitchenObjectSO;
}

public void SetKitchenObjectParent(IKitchenObjectParent KitchenObjectParent)
{
if (this.kitchenObjectParent != null)
{
this.kitchenObjectParent.ClearKitchenObject();
}

this.kitchenObjectParent = KitchenObjectParent;
KitchenObjectParent.SetKitchenObject(this);

if (KitchenObjectParent.HasKitchenObject())
{
Debug.LogError("IKitchenObjectParent already has a kitchen object");
}

transform.parent = KitchenObjectParent.GetKitchenObjectFollowTransform();
transform.localPosition = Vector3.zero;
}
public IKitchenObjectParent GetKitchenObjectParent()
{
return kitchenObjectParent;
}

public void DestroySelf()
{
kitchenObjectParent.ClearKitchenObject();
Destroy(gameObject);
}

public bool TryGetPlate(out PlateKitchenObject plateKitchenObject)
{
if (this is PlateKitchenObject)
{
plateKitchenObject = this as PlateKitchenObject;
return true;
}else
{
plateKitchenObject = null;
return false;
}

}

public static KitchenObject SpawnKitchenObject(KitchenObjectSO kitchenObjectSO, IKitchenObjectParent kitchenObjectParent)
{
Transform kitchenObjectTransform = Instantiate(kitchenObjectSO.prefab);

if (kitchenObjectTransform == null)
{
Debug.LogError("Failed to instantiate kitchen object from prefab: " + kitchenObjectSO.prefab.name);
return null;
}

KitchenObject kitchenObject = kitchenObjectTransform.GetComponent();

if (kitchenObject == null)
{
Debug.LogWarning("KitchenObject component not found on instantiated object.  Adding the component...");

// If KitchenObject component is not present, add it
kitchenObject = kitchenObjectTransform.gameObject.AddComponent();
}

kitchenObject.SetKitchenObjectParent(kitchenObjectParent);
return kitchenObject;
}

public void CheckForLettuceWithoutOther()
{
// Assuming PlateCompleteVisual is attached to a GameObject in the scene
PlateCompleteVisual plateCompleteVisual = FindObjectOfType
();

if (plateCompleteVisual != null)
{
foreach (PlateCompleteVisual.KitchenObjectSO_GameObject kitchenObjectSOGameObject in plateCompleteVisual.kitchenObjectSOGameObjectList)
{
// Perform necessary logic here
}
}
}

[Serializable]
public struct KitchenObjectSO_GameObject
{
public KitchenObjectSO kitchenObjectSO;
public GameObject gameObject;
}

[SerializeField] private PlateKitchenObject plateKitchenObject;
[SerializeField] private List kitchenObjectSOGameObjectList;

private void Start()
{
plateKitchenObject.OnIngredientAdded += PlateKitchenObject_OnIngredientAdded;

}

private void PlateKitchenObject_OnIngredientAdded(object sender, PlateKitchenObject.OnIngredientAddedEventArgs e)
{

foreach (KitchenObjectSO_GameObject kitchenObjectSOGameObject in kitchenObjectSOGameObjectList)
{
if (kitchenObjectSOGameObject.kitchenObjectSO == e.kitchenObjectSO)
{
kitchenObjectSOGameObject.gameObject.SetActive(true);

KitchenObjectType.IdentifyKitchenObject((KitchenObjectSO.ObjectType)(ObjectType)kitchenObjectSOGameObject.kitchenObjectSO.objectType);

}
}

}
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KitchenObjectType : MonoBehaviour
{
public enum ObjectType
{
Bread,
Lettuce,
Cheese,
Tomato,
// Add more types as needed
}

public static void IdentifyKitchenObject(ObjectType objectType)
{
switch (objectType)
{
case ObjectType.Bread:
Debug.Log("Found bread!");
// Handle bread-specific logic
break;
case ObjectType.Lettuce:
Debug.Log("Found lettuce!");
// Handle lettuce-specific logic
break;
case ObjectType.Cheese:
Debug.Log("Found cheese!");
// Handle cheese-specific logic
break;
case ObjectType.Tomato:
Debug.Log("Found tomato!");
// Handle tomato-specific logic
break;
// Add cases for other types as needed
default:
Debug.Log("Unknown kitchen object type!");
break;
}
}

internal static void IdentifyKitchenObject(KitchenObjectSO.ObjectType objectType)
{
throw new NotImplementedException();
}

public static explicit operator KitchenObject.ObjectType(KitchenObjectType v)
{
throw new NotImplementedException();
}
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlateKitchenObject : KitchenObject
{

public event EventHandler OnIngredientAdded;

public class OnIngredientAddedEventArgs : EventArgs
{
public KitchenObjectSO kitchenObjectSO;
}

[SerializeField] private List validKitchenObjectList;
private List  kitchenObjectSOList;

private void Awake()
{
kitchenObjectSOList = new List();
}

public bool TryAddIngredient(KitchenObjectSO kitchenObjectSO)
{
if (!validKitchenObjectList.Contains(kitchenObjectSO))
{
return false;
}
if (kitchenObjectSOList.Contains(kitchenObjectSO))
{
return false;
}
else
{

OnIngredientAdded?.Invoke(this, new OnIngredientAddedEventArgs
{
kitchenObjectSO = kitchenObjectSO
});
kitchenObjectSOList.Add(kitchenObjectSO);

return true;
}
}

}

Я пробовал много разных подходов к этому, но, похоже, не могу понять этого, и, поскольку я новичок в программировании, я решил спросить общественность, спасибо.


Источник: https://stackoverflow.com/questions/781 ... hen-object
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»