- Home /
Stop script immediately from another script
Hi! How can I stop a script from another? I tried to do this: scriptName.enabled = false; but sometimes it doesn't work - script is still executing. Note that I need to stop this script from another so I can't use return
.
What do you mean by it's still executing? It depends on the function, Start(), Awake(), Update(), FixedUpdate(), and OnGUI() that is.
How are you getting the script object?
GameObject object_to_disable_script_on //has been assigned a value perviously
ScriptClass script_to_disable=object_to_disable_script_on .GetComponent< ScriptClass >();
script_to_disable.enabled = false;
Note: the above example will disable the script for only a single game object: object_to_disable_script_on.
If you want to stop it for ALL game objects, that would be different. I would probabaly use a static class to store an enable/disabled value for the whole program. And Then check it inside the script's update function- do nothing (return) if the global enable/disabled value is "disabled".
I'm getting another script and setting enabled field to false properly, but it doesn't work - I've got infinite loop which is doing something, and it's still doing it even when I turn off script.
Just to clarify: enabled flag will NOT ter$$anonymous$$ate an loop running in your script. All it will do is prevent the system from CALLING your scripts update(and possibly other) function AGAIN.
Answer by Digital-Phantom · Mar 23, 2015 at 03:14 PM
Do you have to stop the other script completely? or is there just part of the script that you don't want to execute?
If its variable related just make it static and use your other script to control it.
For example if on the script you want to control there is a Boolean variable, just make that variable static and change the true/false condition from your second script.
Otherwise you may just be better off doing what @Glurth suggested.
oh, that IS much simpler than creating a whole static class, as I suggested in comments!
Answer by Nymisu · Mar 23, 2015 at 03:12 PM
.enabled is in my experience is quite unreliable, and have opted out from using it. Although i haven't tried it in 5, it was very clumsy in 4.6
A very simple way is to just implement an enabled boolean, and do this:
public bool IsEnabled = true;
{...}
void Update()
{
if(!IsEnabled) return; //prevents anything happening after this line
}
Alternatively, you can delete the script if you need it no longer, or want to reinstate it at default settings later with:
gameObject.AddComponent<YourScript>(); //add
Destroy(yourgameobject.GetComponent<YourScript>()); //remove
EDIT: Alternatively, i remembered this relatively expensive-ish workaround to the issue:
(transform.GetComponent<YourScript>() as MonoBehaviour).enabled = false;
Answer by Team_26 · Mar 23, 2015 at 03:32 PM
Thanks for replies. As @Digital Phantom sugggested, I made a public bool variable and at start I set it to false. In Start() function, I'm calling my coroutine. That's the code of it:
IEnumerator DoSomething()
{
while (true)
{
if(enable)
{
// Do something if enable equals true
}
}
}
And then I set enable variable to true or false from other script. There's one problem - Unity doesn't run my game after these changes (there are not any compile errors)... Do You know why?
You have an endless loop in a coprocess. Always avoid while(true), you'll find nothing but woes in that.
Also make very sure you're not calling the coprocess numerous times
If you're satisfied with an infinite loop equal to Update, you can create a coroutine with while(true) encapsulating a yield return null. This will make it run infinitely, but yield to every new frame never being stuck
@Team_26 You don't make infinite loops. Ever. For any reason. Just as you don't use the bool? filetype, or use go to's.
All of these are coding nightmares for very good reasons.
$$anonymous$$ust disagree with @Nymisu: I$$anonymous$$HO, while(true) is a perfectly legitimate loop. There are $$anonymous$$ANY times when you want to check a(or many) condition(s), and based upon them, break, in the $$anonymous$$IDDLE of the loop.
To avoid a particular part of the program$$anonymous$$g language, just because the condition is not on the same line (like a "for" loop), well, I think you'll just end up making certain things more convoluted.