gabbil
1
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.
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));
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!!