これは例ですが、しっかりフリーズします。
フリーズしたらタスクマネージャーから落とすとかしましょう。
ほんま馬鹿。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
int up;
void Start()
{
}
// Update is called once per frame
void Update()
{
up++;
Debug.Log(up);
checkupdate();
}
private void checkupdate()
{
while(up < 100)
{
Debug.Log("UPDATE");
}
Debug.Log("end checkupdate");
}
}
}
よくよく考えると当然のことです。
checkupdate() で参照している up は Update() 内で変更されています。
そして checkupdate() は Update() の中にあり, Unity の 1フレームの間で実行されているので checkupdate() が実行されている間に up が変更されることはないです。
Update() は内部の処理が全部終わってから次のフレームにいくはずなのに、checkupdate() の while の処理が終わらないため
無限ループが起こります。
Update() 内の順番
一回目
up++ (up = 1と考える)
↓
Debug.Log(up) (1が出力)
↓
checkupdate() (ずっと1を参照し続けてしまう)
二回目
up++ (たどりつけない)
↓
........
Coroutine で書くまたは Update内で書く方がいいと思います。
まあ下の処理だと何回も StartCoroutine されるからあんまりよくない気がするけどそのあたりは bool とかいろんな条件をつければいい感じになると思います。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
int up;
void Start()
{
}
// Update is called once per frame
void Update()
{
up++;
Debug.Log(up);
StartCoroutine(checkupdate());
}
private IEnumerator checkupdate()
{
while(up < 100)
{
Debug.Log("UPDATE");
yield return new WaitForEndOfFrame();
}
Debug.Log("end checkupdate");
}
}
}