Search Unity

How to disable the full behaviour of a script?

Discussion in 'Scripting' started by TKDHayk, Dec 2, 2016.

  1. TKDHayk

    TKDHayk

    Joined:
    Dec 22, 2015
    Posts:
    131
    I need to disable a script along with OnTiggerEnter function contrianed within it? As of now, disabling a script does not Disable OnTriggerEnter functions, which is a problem. Is there a way to disable ONtriggerEnter without disabling the trigger of my Gameobject?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Wrap your OnTriggerEnter body with if(enabled){ }
     
  3. TKDHayk

    TKDHayk

    Joined:
    Dec 22, 2015
    Posts:
    131
    Yup I ended up making a boolean "OnTriggerEnterAllowed" then disabling it. thanks!
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You don't even have to do that. enabled is part of Behaviour already and is the thing that gets flipped to false when you disable your script. Everything you need is already there :)
     
    LiterallyJeff likes this.
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Not true. All of the physics engine messages gets called on the script, regardless of wheter it's enabled or not.

    That is incredibly bad design on Unity's side. The UI system has the same problem, where it's kinda random if a component respects .enabled or not.

    The only thing you can trust .enabled to do is to turn off methods with Update in their name.
     
    LiterallyJeff likes this.
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I know - my point was that he could use that boolean instead of creating a new one.

    I agree it's bad design.