While loop causing Unity to hang up?
void OnCollisionEnter(Collision colInfo)
{
if (colInfo.collider.tag == "Infection")
{
Debug.Log("IF STATEMENT");
health = health - 1;
Die();
}
}
void OnCollisionStay(Collision stayInfo)
{
while (stayInfo.collider.tag == "Infection")
{
timer = 0;
timer += Time.deltaTime;
if(timer == 1)
{
health -= 1;
timer = 0;
}
Die();
Debug.Log("WHILE STATEMENT");
}
}
void Die()
{
if (health <= 0)
{
Destroy(gameObject);
Instantiate(infection, transform.position, transform.rotation);
}
}
Not exactly sure why it's having issues... I read to be careful with while statements and this really doesn't seem like it's running infinitely. It runs fine with the if statement in OnCollisionEnter but doesn't work with the while statement in OnCollisionStay.
Answer by eses · Aug 30, 2018 at 06:20 PM
hi @coltonatlas
If you check the manual for OnCollisionStay:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionStay.html
There it says:
"OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider."
So every frame your collision happens, your while loop gets called. Not exactly what you want most likely.
Here is a modified example, that causes typical contact damage.
Each frame player has contact to some specific collider, counter value goes up. After a while timer gets full, and hitpoint is reduced. Timer is zeroed, and starts again.
If player stays in contact long enough, he runs out of energy and dies.
public float health = 10f;
public float timer = 0f;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Infection")
{
timer = 0f;
}
}
void OnTriggerStay(Collider other)
{
// Every frame overlapping infected
SufferInfection();
}
void SufferInfection()
{
timer += Time.deltaTime;
// On timer full, cause damage
if (timer >= 1f)
{
health -= 1f;
timer = 0;
}
// When
if (health <= 0) Die();
}
void Die()
{
Debug.Log("You died!");
}
Well damn. All I'm really trying to do is start a timer while contact between two bodies exist. I figured OnCollisionStay would be the right thing to do. Do you have any advice? Thank you for the info on OnCollisionStay!
@coltonatlas - well you should (like I had to) rubber duck this... i.e. explain the code to yourself, that way you'll eventually figure out what is wrong. Also, work with pseudo code first, that way it is easier to avoid this kind of traps.
I also think, you are better off using trigger. You most likely will bounce away from contact, which will cause timer to stop and so on...
Thanks! That helped a lot. The only issue it's having now is that the timer seems to just stop working after a few triggers. They'll hit and bounce off of each other a few times but will slowly just stay touching due to them going after each other. The timer works fine, then just seems to stop counting after a few triggers happening. :/
Your answer
![](https://koobas.hobune.stream/wayback/20220612173804im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Solved]Cant Find loop that freezes Unity 2 Answers
Why is this Do/While loop hanging Unity? 1 Answer
Do-While Loop not working 1 Answer