Home > OS >  Infinite loop: while(Time.time < Time.time 5f)
Infinite loop: while(Time.time < Time.time 5f)

Time:05-04

I am going insane. For the life of me I cannot work out why the following code is causing Unity to freeze up as soon as I press play. It's an otherwise empty project with the script attached to an empty gameobject. In the console, nothing appears, not even the initial Debug.Log("Step 1");

using UnityEngine;

public class Reels : MonoBehaviour
{    void Start()
    {
        Debug.Log("Step 1");
        TestFunction(1f);
    }

    private void TestFunction(float duration)
    {
        float endTimer = Time.time   duration;
        Debug.Log("Step 2 "    endTimer);

        while (Time.time < endTimer)
        {
            Debug.Log("Step 3");
        }
    }

    void Update()
    {

    }
}

Please save my sanity.

CodePudding user response:

Time.time is a read only field that stores the time at the beginning of the frame, and the way you wrote that code, the frame never ends, because you'll never get out of that loop.

  • Related