Editor Thinks Object has Been Destroyed when it is clearly just disabled in the Hierarchy

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

I suspect that there’s an InventoryItem instance somewhere that is being created and immediately destroyed.

Can I ask how that might happen accidentally? And how might I find out if this is true?

If you are right, I assume that would also account for the fact that only one of these errors are happening?

I forgot to mention this in the post, but I am using a foreach loop I call the ResetData function on the item. Would i see 10 errors or just 1 in that case?

Several ways it could happen but it’s difficult to guess how in this case. Maybe there’s a script that’s instantiating it, or another object that has a slot as its child, and then immediately destroying it. Maybe there’s a scene that’s being loaded and then immediately unloaded.

The reason I suspect this could be happening is that when you null checked one of the instance fields, the next field that was accessed also gave the same error, which suggests that these references don’t exist any more because the game object they’re connected to has been destroyed itself.

If all game objects in the loop had this issue then you’d see multiple errors I think yes, not just 1. But I’m not totally sure about this, since the first error could be causing execution to halt and the loop to stop immediately. Depends how Awake is handled by Unity.

How are you instantiating the objects exactly?

Here’s the Instantiate Code. The position of the prefab is dictated by the horizontal layout group.

The function gets called during Awake

public void IntializeInventory(int inventorySize)
{
    for (int i = 0; i < inventorySize; i++)
    {
        InventoryItem inventoryItem = Instantiate(itemSlotPrefab, Vector3.zero, Quaternion.identity);
        inventoryItem.transform.SetParent(contentPanel); 
        listOfInventoryItems.Add(inventoryItem);
    }
}

This code looks good. Sorry I couldn’t tell you what’s going wrong. I would start looking at any code in the entire project that destroys objects and go from there. Try finding all references for Destroy().

2 Likes