Search Unity

Stopping execution of while loops when disabling a script

Discussion in 'Scripting' started by theinfomercial, Dec 10, 2011.

  1. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    When I try to disable one of my scripts, the while loops in the script are still being executed. Does anyone know how to fully stop a script from running once disabled? Or at least stop while loops?

    I thought disabling a script would stop, or at least pause, all executions on a script. Why isn't this the case?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Disabling a script only disables Update and related functions.

    Code (csharp):
    1. while (something) {
    2.     // stuff
    3.     if (!enabled) break;
    4. }
    --Eric
     
  3. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Oh, I see.

    Edit: Unity should incorporate a script exception called @script ExecuteWhenDisabled. Because that's pretty much how much sense this whole thing makes. When the script is disabled, it should be disabled. No ifs ands or buts.

    Now I have to add the if statements to all my while loop commands that have their scripts disabled.
     
    Last edited: Dec 10, 2011
  4. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    There is no such thing as enable or "disable script" in C#. The enable property is part of the Behaviour (or MonoBehaviour) class and does what Eric said: It tells the engine whether or not to call the Update/FixedUpdate functions on every update or physic update or not.

    If you have a loop, it's your task to make sure it stops!
     
  5. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Oh, it derives from monobehaviour. I was under the impression it was an engine function. Oops.

    Still, though, it would be nice if there was a script preference that could make it so that you would have your loops stop when you disable the script. I would have figured that most people wouldn't want their loops to continue if their scripts containing them were disabled.
     
    Last edited: Dec 10, 2011
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually I depend on loops continuing in disabled scripts, though I'm probably not most people. ;)

    --Eric