I am using this code to equip items on the player:
//Equipping items on the character's model
public void EquipItems()
{
//runs through each location where the items will be equipped on the player character model
for (int i = 0; i < 5; i++)
{
//if the equip location exists
if (itemLocation *!= null)*
-
{*
-
//destroys any existing equipped items if they don't match the newly-equipped items*
_ foreach (Transform child in itemLocation*.transform)_
_ {_
_ if (child != null)_
_ {_
_ if (child.gameObject.name != equippedItems.itemPrefab.name && child.gameObject.tag == “Item”)
{
GameObject.Destroy(child.gameObject);
}
}
}
//places the new items in their locations*
equippedItemsClones = Instantiate (equippedItems .itemPrefab, itemLocation.transform.position, itemLocation*.transform.rotation) as GameObject;
equippedItemsClones.transform.parent = itemLocation.transform;
Debug.Log("instantiating " + equippedItems.itemName);
}
}
}*
But, when I run it, I pick up an item from the ground, try to equip it, and get shown this error message:
MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84)
CharacterClass.EquipItems () (at Assets/scripts/Classes/CharacterClass.cs:94)
InventoryScript.Update () (at Assets/scripts/GUIScripts/InventoryScripts/InventoryScript.cs:63)
I don’t know what’s gone wrong._