- Home /
How can I re-spawn a script component that has been destroyed?
I am using the following code to destroy a script that is a component of a certain game object, but I am not destroying the game object itself:
if (gameObject.name == "Reality Monitor")
{
Destroy(gameObject.GetComponent<WebcamPreview>());
}
How do I "respawn" this script in the object after a certain period of time? Thanks in advance.
do you need to destroy it? You can simply disable it with with SetActive
EDIT: SetActive works on the game object, for the component you simply do component.enabled = false
Answer by LeeroyLin · Jun 21, 2018 at 01:00 PM
You can destroy a component by using Destroy function. Destroy(GetComponent());
And you can add a component by using AddComponent function. gameObject.AddComponent(typeof(WebcamPreview));
Answer by tormentoarmagedoom · Jun 21, 2018 at 02:47 PM
Good day gabbil-
You can not "restore" a destroyed script. You can instantiate a new copy of the script (but all registred data will be reset). With
gameObject.AddComponent<WebcamPreview>();
But, why you want to destroy it if you will need it after? You can just disable it like any other component, with
gameObject.GetComponent<WebcamPreview>().enable = false;
So it will not be active until do make enable true again.
Bye!!
Your answer
![](https://koobas.hobune.stream/wayback/20220612165210im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with a Unity Scripting example from the scripting manual! 0 Answers
Placing a dynamic board in the exact Center of the Camera 1 Answer
Unity Camera and transform? 1 Answer
I'm clueless about how to make my script change its position based on how close it is to the wall.. 0 Answers
Multiple Cars not working 1 Answer