- Home /
OnTriggerExit overwrites OnTriggerEnter
Dear commUnity,
I currently am working on ingame text boxes which should appear when the player enters a trigger and which disappear again when leaving the trigger. However, this is not working properly. When moving the player inside the trigger, the text box only pops up for a frame. I noticed the text box only stays active at the very edge of that trigger. The player moves with a Character Controller and has no ridigbody attached.
This is the code:
var textBox : GameObject; // The linked text box which appears when entering the trigger
function OnTriggerEnter(other: Collider)
{
if (other.gameObject.tag == "Player")
{
// Activate text box
textBox.gameObject.SetActive(true);
}
}
function OnTriggerExit(other: Collider)
{
if (other.gameObject.tag == "Player")
{
// Deactivate text box
textBox.gameObject.SetActive(false);
}
}
I tried using OnTriggerStay as an alternative (of course, this is the first option which comes into mind) but the issue was still there. No effect on the problem.
I can also provide an image of the only position where the text box is active constantly:
(The black cube is the player dummy)
Answer by Nevadaes · Aug 03, 2013 at 12:07 PM
From http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html
Note that trigger events are only sent if one of the colliders also has a rigid body attached.
It might be worth the try to add a rigid body to your character.
No this does not work either. The trigger still only activates at the edge and closes the popup again when moving the player inside the trigger. $$anonymous$$aybe I need to add, I use a Probuilder trigger. However, I don't believe this causing the trouble.
Answer by Lovrenc · Aug 03, 2013 at 12:38 PM
Once your object triggers
OnTriggerEnter
You deactivate it by setting active to false. This means, it is not active anymore and thus wont collide anymore (It wont even be in collision detection loop anymore, but it seems it throws OnCollisionExit uppon deactivation). Try only disabling renderer.
Your answer
Follow this Question
Related Questions
How to use OnTriggerEnter with multiple triggered objects? 1 Answer
On trigger exit not working 2 Answers
OnTriggerEnter/Exit called unexpectedly 2 Answers
Help with OnTriggerEnter issue 3 Answers