- Home /
How to restart/rerun a script?
So I have a script (Script A) that needs to run only when certain input is received. I have been able to enable and disable Script A from another script (Script B - the script were the input is received) using GetComponent().enabled = true/false. I have also been able to do this using a public static boolean "run" in Script A. When Script B receives the appropriate input from the user it sets ScriptA.run = true allowing Script A to run. The problem is that after the first time Script A is run, its Start() function no longer runs (it only runs the one time). For the way my code needs to work, the Start() function needs to rerun each time input is received by Script B. Is there anyway to completely rerun a script such that even the Start() function is rerun?
Answer by phillip71newman · Jun 25, 2018 at 01:14 PM
Wonderful. Works perfectly. Thank you! You can also toggle a script OFF then ON again while the GameObject it is attached to remains active if you use OnEnable().
MyGameObject.GetComponent<MyScript>().enabled = false; //toggle this script to re-invoke it
MyGameObject.GetComponent<MyScript>().enabled = true;