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

标签 c# unity3d

我有两个像这样的 GameObject:

public class GOA : MonoBehaviour
{
    void Start()
    {
     ... do something ...
    }
}

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

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

如何让 GOB:Start() 等到 GOA:Start() 终止?

最佳答案

Start 方法可以是协程。
你可以这样写:

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

    void Start()
    {
        ... do something ...
        IsInitialized = true;
    }
}

这是您的 GOB 脚本:

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 ... 
    }
}

另外不要忘记在 GOB 脚本中包含 using System.Collections.Generic;

关于c# - 如何等待一个 GameObject 实例从另一个 GameObject 实例的 Start with Unity 完成自己的 Start?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52348184/

相关文章:

javascript - 如何减轻GPS追踪主页的压力

c# - 如何为 UIImage 添加渐变阴影

c# - 在 ASP.NET/C# 中将文本附加到标签?

c# - 这些 1k 线程来自哪里

c# - 在 Unity 中使游戏对象可触摸 - 增强现实

c# - 在 C# 中将 char 更改为 int 或 string

c# - 使用 Navmesh 和协程的基本 RTS 运动?

c# - 如何使用相对路径使用 StreamWriter 创建文件?

c# - 如何在 C#.NET 中使用温度性能监视器?

c# - 为什么 "test user-doc.doc"==> TESTUS~1.DOC?