无限循环:while(Time.time < Time.time + 5f)

Infinite loop: while(Time.time < Time.time + 5f)

我快疯了。对于我的生活,我无法弄清楚为什么以下代码会导致 Unity 在我按下播放键后立即冻结。这是一个空的项目,脚本附加到一个空的游戏对象。在控制台中,什么也没有出现,甚至连最初的 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()
    {

    }
}

请拯救我的理智。

Time.timestores the time at the beginning of the frame 的只读字段,按照您编写该代码的方式,框架永远不会结束,因为您永远不会脱离该循环。