- Home /
Destroying object destroyable?
Hey.
I have a game in which you are able to tell another "person" (AI), to move something, to a previously defined marker, that the player can set up. The AI then picks up a specific object, moves to the position, sets it down, and repeat.
Problem is - when the AI sets down the object (unparrents it), i need the item to change. I do this by destroying the object, and instantiating another in its place. Next the marker needs to be removed, so he wont put the next item, on that same mark.
This code is run in the update function, and triggers the "MissingReferenceException" - GameObject destroyed:
GoToPos(ItemToPickup.transform.position);
This is where the object gets destroyed - notice that i even move the object from the variable, initialize the variable, and destroy the object with thetemporary variable - yet i get the error. This code is part of the GoToPos function.
var tmp3 = ItemToPickup;
ItemToPickup = null;
Destroy(tmp3);
// After this ItemToPickup is set to a new object
radius = 30.0;
colliders = Physics.OverlapSphere (transform.position, radius);
for (var hit : Collider in colliders)
{
if (!hit)
continue;
if (hit.name.StartsWith("BarricadeAAP") && hit.tag == "HeavyItem")
{
ItemToPickup = hit.gameObject;
break;
}
}
if (!ItemToPickup)
{
// The function in update stated above, is only run if BarHouse > 0
BarHouse = 0;
HouseToBarri = null;
}
if i after this put in Debug.Log("itemtopickup = " + ItemToPickup); it does show a gameobject. - yet on next update, for some reason its apparently gone..
Answer by Seizure · Sep 23, 2013 at 01:01 PM
tmp3 actually contains the object you are trying to destroy...
Try destroying tmp3 then set ItemToPickup as null
initializing ItemToPickup before or after destroying tmp3 gives the same result