Hello,
I do not understand why the code below does not work for me.
I am trying to instantiate a game object from a game object which I have saved as a class member. The cloned game object should have the same properties as the original with a slightly different mesh (the baked mesh from a particle system). This cloned version should become the original game object in the next update.
When I run this in play mode I get this error: MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
for (int i = 0; i < bPart.Count; ++i)
{
bPGO p = bP[i]; //my class
//... make some changes on p.m_GO
GameObject newGO = Object.Instantiate(p.m_GO, p.m_GO.transform.position, p.m_GO.transform.rotation, p.m_GO.transform.parent);
Object.Destroy(p.m_GO);
p.m_GO = newGO;
}
Originally, I kept the original game object and just changed its mesh properties, but due to various reasons, I might have to create a completely new game object every update.
The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!
In this case, you’re destroying something and then trying to use it again.
Some notes on how to fix a NullReferenceException error in Unity3D
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
To me, it is always simpler to keep an original game object, make clones of it and destroy the clones, then make fresh clones. This eliminates the confusion of line 8 above, where “the old one is now the new one,” which can be baffling to what is going on.
I understand that I have the null reference error because somehow I am destroying the game object saved in the class member p.m_GO.
When I make the assignment at line 8, I thought that I would get the newly instantiated object saved, and the next call will access this saved clone, but I guess it did not work because it was just a local variable. I assume that calling destroy many times on the same game object does not produce any error.
Regarding cloning things, your idea of keeping the original alive, while modifying the instances, is neat and I will try to use that instead.
In the end I am storing the instantiated copies in a list and before baking a new mesh into the original I destroy them and clear the list.
I still have not found out how to actually store one instantiated object into the GameObject variable as a class member (from original post the line 8 is useless and the next call on p.m_GO returns null), but that is probably my inexperience in C# references, which I suspect will not be the same as for C++
What is a bPGO in your first script? If that’s a class that’s fine. If that’s a struct, what you’re doing will absolutely NOT work, because structs are value types and line 3 in that script makes a complete copy to the variable p, leaving the original in the array unmodified by future operations.
It is a struct, actually. I did not know that it makes a hard copy, I feel ashamed
I automatically created a struct thinking it was similar to a class except some privacy stuff.
I have much to learn for C#