Crash when I run a script

When I run this script, unity freezes. Why? How to fix it?

using System.CodeDom;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class WalkingBoss : MonoBehaviour
{
    private GameObject playerOb2j = null;
    private GameObject TXT = null;
    float timeToWait = 0.09f;
    // Start is called before the first frame update
    void Start()
    {
        if (playerOb2j == null)
            playerOb2j = GameObject.Find("Boss");
        if (TXT == null)
            TXT = GameObject.Find("Cam");
    }
    // Update is called once per frame
    void Update()
    {
  
        timeToWait -= Time.deltaTime;
        if (timeToWait <= 0)
        {
            float x = playerOb2j.transform.position.x;
            float z = playerOb2j.transform.position.z;
            float finalx = TXT.transform.position.x;
            float finalz = TXT.transform.position.z;
            while (x!=finalx & z!= finalz)
            {
                transform.position = new Vector3(x+1, 1, z+1);
               
            }
            timeToWait += 0.02f;




        }
    }
}

Your while loop never terminates. You can’t code in Unity that way: until Update() returns, nothing happens. The game is LOCKED SOLID as long as you do not allow the Update() loop to return.

You also generally cannot compare floating point quantities for equality. They may never be precisely equal. Instead compare for distance apart being small enough for your needs.

Here is some timing diagram help:

1 Like

Thank you! I put a return; and it worked!