Placing an object into an array then retriving it after it has been deleted.

I try to move a weapon into my inventory array, then delete the object from the world thinking that a digital copy has been made. when i try to instantiate the object from the object stored in the array, i get the error: MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Here’s my code:

//Weapon.js

//Activate get called by the player when it picks the weapon up
function Activate(sender:Transform){
	inv=sender.GetComponent(Inventory);
	useable=false;
	inv.weapons.Add(this.transform);
	Destroy(this.gameObject);


}

//PlayerBehaviour.js

//Equip get called when i try to create a new weapon at my hand

function Equip(){
var inv:Inventory;
var wep:Transform;
inv=GetComponent(Inventory);
	if(!gameObject.GetComponentInChildren(Weapon)){
		wep=Instantiate(inv.weapons[0],rightHand.position, Quaternion.identity);
		wep.transform.parent=rightHand;
		wep.GetComponent(Weapon).isEquipped=true;
		weapon=wep.transform;
	}else{
		Destroy(weapon.gameObject);
		weapon=null;
	}
}

Can anyone help me out with this? Thanks.

When you add a reference type to an array you don’t add a copy of the object, you just add a reference to the object. This means if you then destroy the object the reference in the array will be null. Why not try deactivating the game object instead using SetActive.

Thanks, it’s solved! :smile: