- Home /
Problem with OnTriggerStay2D function
Hello, everyone. I have a small (but annoying) problem, which stuck me for a while. I am trying to check collisions with OnTriggerStay2D function. I have a bool variable, which shows, whether my object has collided or not. So I need to reset my bool variable to false every frame, before it is modified in this function, but i can't do that! If I reset it anywhere in my code, it resets after the collision check... and also, Start() and Update() functions do not work on this class for me (somehow)... My class is atatched to a prefab. There is the code below.
...
void OnTriggerStay2D(Collider2D ground)
{
if (ground.gameObject.tag == "GroundObject")
{
IsOnGround = true;
}
}
public bool IsOnGround = false; //Creating bool
...
//Where to put 'IsOnGround = false;' ? so it would be redefined every frame before OnTriggerStay2D() checking?
Please help me, if you have an answer or some advice! :)
Thanks.
Answer by Dainiusss · Apr 26, 2015 at 07:58 PM
Solved this problem just by installing unity 5, because I was using much older version of unity 4...
Answer by lordlycastle · Apr 24, 2015 at 08:46 PM
I think you are looking for OnTriggerEnter
, which is triggered only when a collider enters a trigger; so just once. You can set it to set OnGround
to true. The start and update functions should work, is the class subclass of MonoBehaviour. You may be typing it wrong.
If you still wanna stick with you technique, where you set IsOnGround
to false every frame, then it should go in LateUpdate
which is called after each frame has been rendered.
Thanks for advice. I do not want to use OnTriggerEnter and OnTriggerExit, because if my colliders are in contact, and I would delete one collider, OnTriggerExit function will not work. So because of that, my object would be shown to be on ground.