我有一个选手对象下面的代码:产量waitforseconds()不工作
function Start()
{
GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI);
}
function OnCollisionEnter(hitInfo : Collision)
{
if(hitInfo.relativeVelocity.magnitude >= 2) //if we hit it too hard, explode!
{
Explode();
}
}
function Explode() //Drop in a random explosion effect, and destroy ship
{
var randomNumber : int = Random.Range(0,shipExplosions.length);
Instantiate(shipExplosions[randomNumber], transform.position, transform.rotation);
Destroy(gameObject);
GUI.Lose();
}
而且我GUI.Lose()函数如下:
function Lose()
{
print("before yield");
yield WaitForSeconds(3);
print("after yield");
Time.timeScale = 0;
guiMode = "Lose";
}
当爆炸功能所谓的松散函数被调用,并且我看到“yield之前”消息被打印出来。我等了三秒钟,但我从来没有看到“收益率”信息。
如果我拿出产量,这个函数按照我的预期减去等待3秒。
这是在Unity 4上。此代码直接来自我相信是在Unity 3.5上创建的教程。我假设代码在Unity 3.5中工作,因为在网站上没有评论为什么收益不起作用。
我做错了什么蠢事?
谢谢,这让我足够接近完成它。我在Destroy之前放置了yield StartCoroutine(GUI.Lose())。在此之前,我设置了renderer.enabled = false;所以我的游戏对象会隐藏。 Lose()函数完成,然后我的gameObject被销毁。 – 2013-04-29 14:22:57
说明:因为可以使用yield语句在任何点暂停协程的执行。 – Joetjah 2013-04-29 14:25:49