Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

swich off a mesh trigger in code ?

Discussion in 'Scripting' started by shader, Mar 3, 2012.

  1. shader

    shader

    Joined:
    Apr 4, 2009
    Posts:
    253
    I have a trigger mesh volume. How can I switch off the trigger attribute off in code ?

    (I want to do this so I only activate the trigger once)

    Thanks
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  3. Minalien

    Minalien

    Joined:
    Feb 10, 2010
    Posts:
    8
    Your best bet is to set a boolean value to flag whether the trigger's been executed already in its script:

    Code (csharp):
    1. public class YourClass : MonoBehavior {
    2.      private bool m_run = false;
    3.  
    4.      void OnTriggerEnter(Collider col) {
    5.           if(m_run)
    6.                return;
    7.  
    8.           m_run = true;
    9.  
    10.           // Rest of your trigger code
    11.      }
    12. }
    Setting collider.isTrigger to false is another option, but you might run into some issues depending on the needs of your game. (For example, if you've got a rigidbody attached to it, it'll end up creating undesired collisions). You shouldn't run into any performance issues with simply setting a boolean value, but another option would be to set collider.enabled to false if you don't want any calls to the trigger functions and wanted to achieve the same effect.
     
    Last edited: Mar 3, 2012
  4. shader

    shader

    Joined:
    Apr 4, 2009
    Posts:
    253
    Thank you both, very useful !

    @ mgear, adapted it to Javascript and it worked fine - cheers
     
    Last edited: Mar 3, 2012