- Home /
Changing Meshes - MissingReferenceException
Hello folks,
I hit a snag while working on a Unity Scene that serves as a garage for tank units where players can pick and choose among three different types of meshes: 1) tank body, 2) turret, and 3) gun. In the scene, I have a 3D rendition of the tank along with buttons so that the player can click to cycle through the various tank part meshes. And, of course, the rendered tank gets a real-time visual update depending on the player selection. Things seem to be working just fine initially.
But, when the player chooses a different turret (meaning, the tank body and gun remain the same), I get the following error:
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.
As for the structure of the tank unit itself, the 'tank body' gameobject has a 'turret mount' where the 'turret' gameobject is placed as a child of the 'tank body'. Subsequently, the 'turret' gameobject has a 'gun mount' where the 'gun' gameobject is placed as a child. So, the object hierarchy is two-level deep.
As for my code, it is not in a single file so I'd rather not just copy the whole thing here but let me do my best to give you what's happening in the background (I will be happy to add code snippets later if necessary):
1) I initialize each part (body, turret, gun) based on a set of default choice. Simplified code looks something like the following:
GameObject obj = Instantiate(gameobject, transform.position, Quaternion.identity) as GameObject;
obj.transform.parent = transform;
obj.transform.rotation = transform.rotation;
2) When I try to change the 'turret', what I do first is to look at the 'turret mount' and destroy all child objects so that only the new turret choice will be visible, not the default one:
if (turretmount.transform.childCount > 0)
for (int cnt=0; cnt < turretmount.transform.childCount; cnt++)
Destroy(turretmount.transform.GetChild(cnt).gameObject);
This destroys all child objects below the 'turret mount' so the 'turret' and the 'gun' gameobjects get destroyed.
At this point, I use the code routine I mentioned in Step #1 to re-instantiate the 'turret' gameobject (which the player just selected) and the same 'gun' gameobject that was previously used. It is at this point I run into the above error.
When I comment out the code part that re-instantiates the 'gun' gameobject, I get no error whatsoever. Switching from one turret to another works just fine. The error is thrown when I try to re-instantiate the 'gun' gameobject.
Lastly, I did quite a bit of research on the aforementioned error so I kind of understand why this error occurs (the GameObject is destroyed so it is now pointing to an object that is no longer there) but I had no luck with finding a fix for my problem.
Any tips/help/insights will be greatly appreciated!
i don't really entirely understand your problem in detail but ...
(1) just add the new one, AND THEN remove the old one.
(2) try the age-old video game engineering trick of just $$anonymous$$OVING THINGS OFFSCREEN !
as a TEST, just $$anonymous$$OVE the old turret 5 meters to the right (change .localPosition), pause and have a look if that's working. now add the new turret (correct location). pause and check out the situation. now try to delete, disable, destroy, whatever the old one .. again check out the situation
hope helps
Your answer
Follow this Question
Related Questions
Destroy and rebuild game objects from lists 1 Answer
Destroying Prefab Also Destroys Prefab Reference 1 Answer
Losing references after reloading the scene, even though they exist again. 1 Answer
Rescaling of grid and tilemap causes it not to destruct properly 0 Answers
Pass a copy of a GameObject as variable to another script? 1 Answer