- Home /
GameObject reference breaks
So i have this gameobject thats a simple go with a sprite renderer that i use to display where you can cast a spell in a grid. When you click on an ability i instantiate multiple times that gameobject and then place them around on the grid so you know what the area of effect of the ability. I also use them in object pooling no to instiante all the time.
Problem i have is that for some reason the first i click play everything works fine. If i press play again to stop unity it seems the gameobject that is used to instantiate gets garbage collected or something because the next time i press play and instantiate from that object its all broken, i debugged and looked at lets say its transform and its a null ptr exception. So between play in the editor the game object gets disposed but i still have a reference on it and then when i instantiate anything from it its already broken. Anyone has any idea how that would be possible ? I do not have any code that calls dispose or destroy on that gameobject or on any of its copies, the only thing i do is SetActive(false) on the copies.
Answer by FlaSh-G · Aug 10, 2021 at 03:12 PM
The only thing I can think of is that you are overwriting the reference to the prefab with a reference to a clone, which works fine at first because the clone is identical to the prefab. Then, when the clone gets destroyed, the field is referencing a destroyed object. That theory requires you to use some ScriptableObjects or something, because having that field in the scene wouldn't pose a problem, as any changes to its value will be reset after exiting play mode.
Better guesses are hard to make without seeing code.