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.

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.

.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;

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?

this should do the trick Destroy(GetComponent());