Search Unity

Should OnTriggerEnter be getting called even when the script is disabled?

Discussion in 'Editor & General Support' started by applejuice512, Apr 14, 2014.

  1. applejuice512

    applejuice512

    Joined:
    Jan 29, 2014
    Posts:
    2
    I have an object that is a trigger. On this object I have a script attached containing the function OnTriggerEnter(). In OnTriggerEnter() I have a debug.log statement included. When I disable the script the statement still gets printed out when the object is triggered. Is this supposed to be happening? I would've thought that disabling the script would prevent OnTriggerEnter() from firing.
     
    blox5000 likes this.
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, only the various Update functions (and OnGUI) are affected by .enabled. It's intended that other functions like OnTriggerEnter always remain active so that scripts can "wake themselves up" easily.

    --Eric
     
  3. applejuice512

    applejuice512

    Joined:
    Jan 29, 2014
    Posts:
    2
    Gotcha! Thanks!
     
  4. OceanBlue

    OceanBlue

    Joined:
    May 2, 2013
    Posts:
    251
    Well that's a pain in the posterior. Damn. Is there any way around this? I NEED my enemies to be asleep and wake up when Triggered.... but there are so many enemies, some can trigger earlier than others (and out of order), rendering the stuff in OnTriggerEntry on those enemies useless.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (javascript):
    1. function OnTriggerEnter () {
    2.     if (!enabled) return;
    3.     // other stuff
    4. }
    --Eric
     
  6. OceanBlue

    OceanBlue

    Joined:
    May 2, 2013
    Posts:
    251
    Legend - thank you.
     
  7. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    I never thought about it that way. Nice! I always disabled the entire object when I didn't want triggers to trigger. Now I know a better way. Thanks!
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    For reference:

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html

     
  9. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Sorry, I accidentally bumped this topic but deleted my post.