- Home /
How do I enable an object when it is destroyed?
Hi, I am trying to enable an object when it has been destroyed previously. What I am trying to do it turn off a script temporarily and then be turned on again after the player presses a key. Is there another script to disable and enable scripts. I searched through the API and the tutorials and couldn't find anything. I have tried using "SetActive" but that only works for gameobjects. Thank you in advance.
say i have a gameobject(player, with script xyz ) and from other gameobject (main, with script Abc) i want to enable/Disable the player script .then i would do player.xyz.enabled=false;
Answer by Duskp · Jun 11, 2015 at 09:14 AM
You can enable or disable Behaviours be setting the "enabled" bool.
In your case, for example, running the code on the ABC script:
player.getComponent<xyz>().enabled=false; // disable the script running on the player
gameObject.getComponent<abc>.enabled=false; //disable the abc script
You might want to simply use gameObject.Destroy() if you're not using the object for anything else, though.
Answer by Kiwasi · Jun 11, 2015 at 08:14 AM
Destroyed means it's gone forever. No take backs. You can set enabled to false instead. Or you can use AddComponent to recreate the component.
This is not entirely true. I had a student once who thought it was a good idea to use WeakReference to revive destroyed objects and it worked. It was wrong but working.
Destroy does not remove from memory, it sets as useless and remove all references. If you call the GC right after then yes, most likely gone. But if you let it happen as you should, the item will stay in memory until space is needed and GC is called by compiler. During that time, you can zombiefy your object making it alive again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200827im_/https://answers.unity.com/themes/thub/images/avi.jpg)