如何等待一个 GameObject 实例从另一个 GameObject 实例的 Start with Unity 完成自己的 Start?

Ran*_*ize 2 c# unity-game-engine

我有两个GameObject这样的:

public class GOA : MonoBehaviour
{
    void Start()
    {
     ... do something ...
    }
}
Run Code Online (Sandbox Code Playgroud)

另一个对象以这种方式依赖于第一个对象:

public class GOB : MonoBehaviour
{
    void Start()
    { 
     // wait GOA has terminated own "Start" life cycle
     ... then do something ... 
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能GOB:Start()等到GOA:Start()终止?

Md *_*ani 10

Start method can be a coroutine.
You can write something like this:

public class GOA : MonoBehaviour
{
    public bool IsInitialized { get; private set;}

    void Start()
    {
        ... do something ...
        IsInitialized = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

And Here's your GOB script:

public class GOB : MonoBehaviour
{
    public GOA aInstance;
    IEnumerator Start()
    { 
     // wait GOA has terminated own "Start" life cycle
     yield return new WaitUntil(() => aInstance.IsInitialized);
     ... then do something ... 
    }
}
Run Code Online (Sandbox Code Playgroud)

Also don't forget to include using System.Collections.Generic; in GOB script.