Search Unity

OnMouseDown() and enabled = false;

Discussion in 'Scripting' started by the_gnoblin, Aug 19, 2009.

  1. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Hello!
    There are both OnMouseDown() and Update() in my script.

    If I turn the checkbox on my script in inspector (disabling it) - OnMouseDown continues to work...

    So, for instance:
    I create a cube, put a script on it, disable the script, click on cube and get a message from my OnMouseDown() ("cube clicked!").

    Am I doing something wrong?).
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Disabling a script really only disables Update but not much else.

    --Eric
     
  3. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Damn, I thought disabling a component is simple :D
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Oh but it is simple! You just have a boolean called "onOff" inside the script that has to be true for anything to happen inside functions like OnMouseDown(). Then you just set enabled = false and onOff = false whenever you don't want it doing anything.
     
  5. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Thanks, I am aware of OnOff :D .
     
  6. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    That's way too complicated, if you ask me. Why not just do this:

    Code (csharp):
    1.  
    2. function OnMouseDown()
    3. {
    4.    if (enabled)
    5.    {
    6.       // code here
    7.    }
    8. }
    9.  
     
  7. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Jim Offerman, quite a nice idea!
    :)
     
  8. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Only one reason, really. Sometimes you want it to respond to different stimuli but you want Update() to be turned off temporarily.

    With the onOff technique, you could still have it reporting data, for instance.

    But yeah, if all you want to do is turn it off, if (enabled) is the most streamlined.