My game freezes for unknown reason

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class animacja1 : MonoBehaviour
{
    public GameObject Colliderrury;
    public float timer = 0;
    
    
    Collider2D col;
    // Start is called before the first frame update
    void Start()
    {
        col = GetComponent<Collider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        while (Pipewejscie.animacjarura1 == true)
        {
            if(timer <5)
            {
                timer += Time.deltaTime;
                Debug.Log("Dziala");
                col.isTrigger = true;
            }
            else
            {
                col.isTrigger = false;
                timer = 0;
            }
        }
        
    }
}
````
I dont know why but when i try to activate this by setting animacjarura1 to true whole unity freezes and i have to use task manager to close it.  I ran out ideas please help.
1 Like

Looks like an endless loop to me. Either set Pipewejscie.animacjarura1 to false somewhere or use an if statement instead a while loop.

1 Like

while is not a construction that is suited to be placed in Update().
You got expected behavior.

timer can be counted in Update directly using if statement.

Collider operating must be placed in the FixedUpdate() as well as other operating with Physics.

It’s very dangerous to use While loop. It probably goes into endless recursion and it just hangs. Refactor your code.

1 Like

tbh i just changed the way i did it to check if player has touched the ground and if he did col would stob being a trigger everything worked flawlessly