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.