The object of type 'Transform' has been destroyed but you are still trying to access it (except I never destroy anything)

So here is my issue, I am trying to create an inventory system by myself and I want to instantiate a image of an item my character has obtained directly to my inventory UI, a prefab that is connected to an ScriptableObj, but everytime the issue in the title shows up. It works, the image of my item shows up in my inventory, it loops excessively, but I can fix the looping. Nothing in the scene is ever destroyed, I never call the function Destroy(); and I used the ‘if (this != null)’ as required, and yet still the same issue. I just want to know what is wrong here.

here the inventory code:

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

public class Inventory : MonoBehaviour
{
    public Itens[] itens;  
    public InventorySlot[] inventorySlot;
    public int itemNum = 0;
    public GameObject inventoryItemImage;

    public void AddItemToSlot ()
    {
        foreach (InventorySlot slot in inventorySlot)
        {
           if (slot.hasItem == false)
           { 
               
                 if (slot.itemSlot != null)
                 {
                        foreach (Itens item in itens)
                        {
                            if (item != null)
                            {
                                inventoryItemImage = Instantiate(item.itemImage, slot.itemSlot);

                            }
                        }
                 }                
                
            }
            
        }
    }
    void Update()
    {
        DontDestroyOnLoad(this.gameObject);
        foreach (Itens item in itens)
        {
            if (item != null)
            {
               AddItemToSlot();

            }
        }
    }
    
}

[System.Serializable]
public class InventorySlot
{
    public RectTransform itemSlot;
    public bool hasItem;
}

here the item collection code (this code also does other things, like check if a character is colliding, check for a mouse click, it animates the player as if it’s collecting an item, things related to the idea of collecting an item)

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

public class CharInteraction : MonoBehaviour
{
    bool isInRange = false;
    bool clicked = false;
    bool collected = false;

    public GameObject inventory;
    
    public Itens itemID;
    public GameObject parent;
    public bool animate = false;
    public bool disapear = false;
    public UnityEvent interact;
    

    GameObject playerStop;
    Animator playerInteraction;
    
    Vector3 mousePosition;
    

    void Start()
    {
        Physics.queriesHitTriggers = true;
        inventory = GameObject.FindWithTag("Inventory");
       
        
    }

    void IsClicking ()
    {
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        

        if (mousePosition.x < parent.transform.position.x +2 && mousePosition.x > parent.transform.position.x - 2 && Input.GetMouseButtonDown(0))
        {
            clicked = true;
            

        }
    }
    

    void Update()
    {
        IsClicking();
        StartCoroutine(InteractionAnimation());
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            isInRange = true;            
            playerStop = collision.gameObject;
            playerInteraction = collision.GetComponent<Animator>();
            

        }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            isInRange = false;
            
        }


    }

    void hasBeenCollected ()
    {
        if (collected == false)
        {
            inventory.GetComponent<Inventory>().itens[0] = itemID;            
            collected = true;
        }
    }

    IEnumerator InteractionAnimation ()
    {
        if (clicked == true && isInRange == true)
        {
            var stop = playerStop.GetComponent<CharacterMovement>();
            stop.enabled = false;
            
            if (animate == true)
            {
                playerInteraction.SetBool("IsInteracting", true);
                yield return new WaitForSeconds(1);
            }
           
            interact.Invoke();
            if (collected == false)
            {
                hasBeenCollected();
                
            }
            clicked = false;
            if (animate == true)
            {
                playerInteraction.SetBool("IsInteracting", false);
            }
            
            stop.enabled = true;
            if (disapear == true)
            {
                parent.SetActive(false);
            }
            
        }
    }


}

here is my scriptable obj

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

[CreateAssetMenu(fileName = "Item", menuName = "ScriptableObjects/Item", order = 1)]

public class Itens : ScriptableObject
{
    public string itemName;
    public string itemDescription;
    public GameObject itemGameObject;    
    public GameObject itemImage;   

}

Ok, no one helped me, so I helped myself, here for anyone having the same issue:

  • Apparently the foreach loop wasn’t quite closed off so I needed to use a for loop instead
  • I created a new variable in the Scriptableobj that checked if it was collected and created an int that assured a consistent loop
  • I added the Itens[] (as a Itens without the []) inside the InventorySlot[] so that each new array element in InventorySlot[] had a single scriptableobj slot so that I didn’t had to loop trought two things
  • I used break; to assure that if the itemslot had a scriptableobj in it the loop would only run one time.
  • I separated my item Image from the scriptableobj in a new gameObj in my item collection code because it still wasn’t working.

At one moment I reset the entire array in my unity Inspector and tested that out and it worked, no issues at all! All done without using internet tutorials! :partying_face:
Now I can set animations for my inventory additions, I can add a tab with descriptions and added information!