- Home /
Is it Possible to Bring back a Destroyed GameObject?
Lets say I destroy a gameObject in a level (when I kill the enemy)
Is it possible to bring back that same gameObject or a copy of it with the same stats as the original or the original itself (such as re spawning?)
Currently I am trying to make my level reset with only slight differences if the player buys upgrades so for that I need the enemies to respawn is this possible?
should I avoid destroying the game Objects?
Thanks
Answer by DaveA · Jan 08, 2011 at 07:07 PM
You might try just deactivating it, and/or put it on a different layer (hidden) and/or put it on a 'graveyard' list which would actually be destroyed when you really don't need it.
Yea I think I'm gonna just move the object to a different off scene location, it seems to be the simplest location since the scene itself isn't using many draw calls and everything seems to render fine either way :) thanks
Answer by The_r0nin · Jan 08, 2011 at 07:15 PM
Try using Prefabs. You can destroy them and then instantiate exact copies.
I can't use a prefab because i have many scripts attached to this object but I think I have figured out what I am going to do
You do realize that you can put scripts on prefab objects, right? $$anonymous$$y entire player, including the main camera and all of the scripts, is one prefab object...
Instantiating a prefab will bring it in with its defaults. I think he wants to keep all the 'current' variable values at the time of death, and resurrect that (without having to store all that off into another place)
Oh... that's not how I read it. That's a completely different solution entirely...
I gotta guess that's what he meant by 'same stats' cuz otherwize, prefab instantiate is the simplest for sure
Answer by Statement · Jan 08, 2011 at 07:50 PM
Lets say I destroy a gameObject in a level (when I kill the enemy)
Is it possible to bring back that same gameObject or a copy of it with the same stats as the original or the original itself (such as re spawning?)
You can't (as far as I know) "undo" a destroy and have internal state intact without having to write your own code that safe-keeps state. You can probably also kiss coroutines goodbye if you need that explicit control since I don't know of any way to restore coroutine internal state.
I think the best bet is to spawn prefabs and ignore loss of state (per ronins suggestion) or put objects in a graveyard list (per daves suggestion).
Since you're reseting mid-game events I guess you'd want to go with the graveyard idea or write some sort of object state manager, but that can become quite complex and may put serious constraints on the remainder of your architecture.
Um it is only one Object, and I technically only need it gone for a short while.
So in retrospect I think I am going to move it to another layer or place out of the scene view and just move it back when the player plays the level again.
Would it be simpler to just set go.active = false, then reactivate it later?
I guess it depends on the context but I'd certainly think so.