I’m working on making an inventory system in Unity. I’m using prefabs to store the item ‘slots’ (Where the images are stored). What happens though is when i call the ResetData function on the slots, the Editor throws a missing reference exception saying that the Game Object I’m accessing is destroyed, except that in the hierarchy the object is clearly not destroyed, just disabled(The error happens on line 19).
Not only that, I’ve tried to troubleshoot using functions like IsActiveInHierarchy but I also get the exact same error for some reason. Even when manually setting the object to not be disabled it still calls the error. I’ve used OnDestroy to check if the object has been destroyed and it simply hasn’t. I have no idea how to fix this bug, though I’m pretty sure somethings wrong with my code.
Also, I’ve use != null to check if the object is null, but then I get an error on the Deselect Function on the ilne where is says ‘borderImage.enabled = false’.
Also keep in mind that I’m setting up these references in a Prefab if that changes anything.
For anybody wondering I’m using Sunny Valley Studio’s tutorial for most of this(With a few tweaks to fit my currently existing project)
public class InventoryItem : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IEndDragHandler, IDropHandler, IDragHandler
{
public Image itemIcon;
public TMP_Text quantityText;
public Image borderImage;
public bool empty = true;
public GameObject itemIconObject;
public event Action<InventoryItem> OnItemClicked, OnItemDroppedOnSlot, OnItemBeginDrag, OnItemEndDrag, OnItemRightClicked;
public void Awake()
{
ResetData();
Deselect();
}
public void ResetData()
{
itemIconObject.SetActive(false); //ERROR IS HERE
Debug.Log("Reset Image");
empty = true;
}
public void Deselect()
{
borderImage.enabled = false; != NULL ERROR IS HERE
}
public void SetData(Sprite sprite, int quantity)
{
Debug.Log("DataSet");
itemIconObject.SetActive(true);
itemIcon.sprite = sprite;
empty = false;
quantityText.text = quantity.ToString();
}
public void Select()
{
borderImage.enabled = true;
Debug.Log("Why");
}
Please feel free to ask for more information regarding what the rest of my project looks like