我有两个像这样的 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/