Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Team_26 · Mar 22, 2015 at 08:21 PM · stopenabledexecutiondisabling

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Team_26 · Mar 23, 2015 at 02:37 PM 0
Share

Nobody knows? :(

avatar image hexagonius · Mar 23, 2015 at 02:42 PM 0
Share

What do you mean by it's still executing? It depends on the function, Start(), Awake(), Update(), FixedUpdate(), and OnGUI() that is.

avatar image Glurth · Mar 23, 2015 at 02:57 PM 0
Share

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

avatar image Team_26 · Mar 23, 2015 at 03:21 PM 0
Share

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.

avatar image Glurth · Mar 23, 2015 at 03:28 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Mar 23, 2015 at 03:30 PM 1
Share

oh, that IS much simpler than creating a whole static class, as I suggested in comments!

avatar image
1

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nymisu · Mar 23, 2015 at 03:35 PM 0
Share

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

avatar image Team_26 · Mar 23, 2015 at 03:36 PM 0
Share

So how can I make endless loop in any function?

avatar image hexagonius · Mar 23, 2015 at 03:55 PM 0
Share

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

avatar image Nymisu · Mar 24, 2015 at 09:27 AM 0
Share

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

avatar image Glurth · Mar 24, 2015 at 02:03 PM 1
Share

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

Show more comments
avatar image
0

Answer by Julio_ · Mar 23, 2015 at 09:59 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I stop executing a Function in a void Function? 1 Answer

Walking Animation 1 Answer

Unity exported application stops when its not on focus, how can I disable this? 1 Answer

How do you stop you gun majicaly sinking into walls? 1 Answer

Why does my function play sounds on startup? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges