【Unity】StartメソッドUpdateメソッドの実行タイミング

やりたい事

Startメソッドで初期値や必要なデータを読み込みたいので読み込むまではUpdateメソッドには待機しててもらいたいです。

背景、状況

Unityでの開発をしています。
バージョンは2021.3.26f1

Startメソッドの動きたがイマイチ分からないので実験しています。
現状はStartメソッド内のfor文が入る前にUpdateメソッドが動いてしまう。

Source

c#

1using System.Collections;2using System.Collections.Generic;3using UnityEngine;4using System.Threading.Tasks;5 6public class Test02 : MonoBehaviour 7{8 9 bool flag = false;10 // Start is called before the first frame update11 async void Start()12 {13 StartCoroutine(E());14 await AMethod();15 Invoke("DelayMethod", 1.0f);16 for (int i =0; i <= 10; i++)17 {18 if(i == 10)19 {20 Debug.Log("終わり");21 }22 }23 }24 private void DelayMethod()25 {26 Debug.Log("Invoke");27 }28 private async Task AMethod()29 {30 await Task.Delay(1000);31 Debug.Log("DelayMethod");32 }33 void Update()34 {35 if (!flag)36 {37 Debug.Log("Update開始");38 flag = true;39 }40 }41 private IEnumerator E()42 {43 Debug.Log("E メソッド");44 for (int i = 0; i < 5; i++)45 {46 yield return new WaitForSeconds(1.0f);47 }48 Debug.Log("E 終了");49 }50}

実行結果

イメージ説明

質問1 そもそもStartメソッドが終わってからUpdateが動くのではないかと思っていましたが違いますか?
※非同期なものがあったとしてもFor文が終わる前にUpdateが入っているのが府に堕ちません。

一応、解決策としてはenabledをfalseにしてからtrueに戻す方法ですが問題ないでしょうか?

コメントを投稿

0 コメント