所以我尝试这样做:
void Update()
{
if (isWalkingTowards)
{
if (Vector3.Distance(character.transform.position,this.transform.position)<0.7)
{
**StartCoroutine(Example());**
Instantiate(character, new Vector3(-6, 0, -1), Quaternion.identity);
Destroy(character);
}
}
}
**IEnumerator Example()
{
yield return new WaitForSecondsRealtime(5);
Debug.Log("This function should be read before it is instantiated or destroyed");
}**
但它只是实例化并销毁对象,然后它向我显示该消息!
答案 0 :(得分:1)
我应该这样做:
void Update()
{
if (isWalkingTowards)
{
if (Vector3.Distance(character.transform.position,this.transform.position)<0.7)
{
**StartCoroutine(Example());**
}
}
}
**IEnumerator Example()
{
yield return new WaitForSecondsRealtime(5);
Debug.Log("This function should be read before it is instantiated or destroyed");
Instantiate(character, new Vector3(-6, 0, -1), Quaternion.identity);
Destroy(character);
}**
答案 1 :(得分:0)
这里有一个超级有用的链接,介绍了多种统一等待的方法:How to make the script wait/sleep in a simple way in unity
此外,我认为您目前遇到的问题是代码不会等待 StartCoroutine(Exmaple())
完成。您必须将要执行的代码前后放在 Example
中。