While loop freezes game in Unity3D

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

I've always struggled with while loops because they barely ever work for me. They always cause my Unity3D application to freeze, but in this instance I really need it to work:

bool gameOver = false; bool spawned = false; float timer = 4f;  void Update ()  {     while (!gameOver)     {         if (!spawned)         {             //Do something         }         else if (timer >= 2.0f)         {             //Do something else         }         else         {             timer += Time.deltaTime;         }     } } 

Ideally, I want those if statements to run as the game runs. Right now it crashes the program and I know it's the while loop which is the problem because it freezes anytime I uncomment it out.

回答1:

Update() is called every frame, so you should not use a while loop in it except in exceptional circumstances. This is because the game screen freezes until the loop is exited.

Further reading: https://docs.unity3d.com/Manual/ExecutionOrder.html

Instead, either use a coroutine as @Programmer has done, or use an if/switch statement instead, with a boolean check.

i.e.

bool gameOverActionDone = false; void Update ()  {     if (!gameOver && !gameOverActionDone)     {         if (!spawned)         {             //Do something             gameOverActionDone = true;         }         else if (timer >= 2.0f)         {             //Do something else             gameOverActionDone = true;         }         else         {             timer += Time.deltaTime; //either keep this here, or move it out if the if condition entirely         }     } } 


回答2:

If you want to use a variable to control a while loop and wait in that while loop then do it in a coroutine function and yield after each wait. If you don't yield, it will wait too much and Unity will freeze. On mobile devices like iOS, it would crash.

void Start() {     StartCoroutine(sequenceCode()); }  IEnumerator sequenceCode() {     while (!gameOver)     {         if (!spawned)         {             //Do something         }         else if (timer >= 2.0f)         {             //Do something else         }         else         {             timer += Time.deltaTime;         }          //Wait for a frame to give Unity and other scripts chance to run         yield return null;     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!