- Home /
Varaible not decementing OnTriggerExit
Hi,
I added a rigid body to my player that I can control and move around the arena. I have the following C# Script which detects if my player has 'stayed' or 'exited' the Plane game object:
public float x = 0;
//...
void OnTriggerStay(Collider other) { if ( x <= 50 ) x++; }
void OnTriggerExit(Collider other) { // does happen when below is commented but not vise versa Debug.Log("outside of Plane");
//if ( x < 50 && > 0 ) x--; }
Just wondering why.
What I don't want to do is this:
//...
bool isHere = false;
void UpDate()
{
if (isHere == false) x--;
}
void OnTriggerStay(Collider other)
{
isHere = true;
}
void OnTriggerExit(Collider other)
{
Debug.Log("leaving plane..");
isHere = false;
}
Which works, but can this be done only in the subrountine OnTriggerExit? I am all ears.
If you don't want the Update, you could always just start a coroutine with OnTriggerEnter and stop the coroutine with OnTriggerExit.
Answer by dhendrix · May 22, 2010 at 04:38 AM
It seems like you think "OnTriggerExit" is called repeatedly if the object is not in the trigger. That is not the case, as it is only called once on the same frame the object leaves the trigger.
Therefore you will have to use a boolean to control the logic flow similar to what you have in the second set of code. Also if this is for points or something similar you should use something like Time.time or Time.deltaTime to do this over time, otherwise it will happen every frame causing the incrementing/decrementing to be frame dependent rather than time dependent, and very fast in most cases(shooting to 50 or zero in less than a second).
Your answer
Follow this Question
Related Questions
OnTriggerExit not working reliably 2 Answers
Check that an object is within the bounds of a trigger, but not touching them? 3 Answers
On trigger exit not working 2 Answers
On trigger exit: player speed value not changing, why?? 1 Answer
How to destroy an instantiated prefab object and keep instantiating it 1 Answer