- Home /
The question is answered, right answer was accepted
Destroy and rebuild game objects from lists
Hello everyone, I am trying to add checkpoints to my little game. For that reason I tried to save all objects with a given tag in my scene to a list and upon death wipe the scene of all objects and instantiate the objects I saved in the list.
making an array with GameObject's and filling it with FindObjectsWithTag does not work here since when I destroy the game object in the scene and try to instantiate it from the array, the object in the array will try to access the destroyed object in order to rebuild it.
Basically i get that my list stores only references to the objects (right?). But is there a way to store the actual objects in a variable so they can be used for instantiating later on?
Instantiating a prefab would be a workaround but considering the parameters I would have to save and reassign to each object,since all the objects are variations of a prefab, I am afraid it is by far not the best way to tackle this problem.
thanks in advance,
NeverGod
I agree with @fafase, but if you did want to actually store them then you would need to Instantiate them into the list. Instantiate basically just copies them.
Both these comments combined are the right answer.
You need to clone each object with Instantiate(originalObject... and then call .SetActive(false) on the clones before you add them to the list.
Thanks a lot! Setting them to inactive and reactivating them worked like a charm! I wasn't aware of that little bool, I've been only using Unity since 2 days, thanks a lot!
Answer by fafase · Apr 12, 2013 at 07:48 PM
Why don't you jsut deactivate them with SetActive? You could even have an additional boolean that says if they were actually destroyed or not.