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
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
Your best bet is to set a boolean value to flag whether the trigger’s been executed already in its script:
public class YourClass : MonoBehavior {
private bool m_run = false;
void OnTriggerEnter(Collider col) {
if(m_run)
return;
m_run = true;
// Rest of your trigger code
}
}
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.
Thank you both, very useful !
@ mgear, adapted it to Javascript and it worked fine - cheers