- Home /
My game frozzes when I use a while to check if there's colision
Ok so I'm still having the problem whit the jump scrip([last post][1]) so I almost fixed it but now when I press my UI button for it my unit frizzes and I have to launch it up again! Heres my Code:
public void GroundCheck(Collider2D coll){
// ground is self explained I hope...
while (coll.gameObject.name == "ground") {
rigidbody2D.velocity = transform.up * 15;
}
[1]: http://answers.unity3d.com/questions/862880/disable-jumping-more-than-once.html
You don't have an exit check. You haven't included a condition check or a way to exit the while loop and that is why it locks in there. That object's name is always ground, so it never gets out of the loop. Try to use an if check on a CollisionStay or TriggerStay function ins$$anonymous$$d.
Answer by Thom Denick · Dec 27, 2014 at 10:02 PM
There's no exit for your while loop. This will cause Unity to freeze. Use "Update" instead of while loops.
Answer by LewiksR · Dec 28, 2014 at 10:34 AM
Use this instead
void OnCollisionEnter2D (Collider2D col) {
//code here
}
@LewiksR Did that but off topic when i click my UI he jumps normally but even if hes not touching the ground he can still jump in air... here's what I did:
public void OnCollisionEnter2D(Collider2D col) { if (col.gameObject.name == "ground") { rigidbody2D.velocity = transform.up
15; } }
Your answer
![](https://koobas.hobune.stream/wayback/20220613175017im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Ground Detection Lagging - 2D 2 Answers
Sprite Sheet animation without fancy add-ons 1 Answer
if it's you, how will you solve it ? 1 Answer