Unity3d yield waitforseconds()不工作

Unity3d yield waitforseconds()不工作,unity3d,yield,unityscript,Unity3d,Yield,Unityscript,我在播放器对象中有以下代码: function Start () { GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI); } function OnCollisionEnter(hitInfo : Collision) { if(hitInfo.relativeVelocity.magnitude >= 2) //if we hit it too hard, explode! {

我在播放器对象中有以下代码:

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";
}
当explode函数被调用时,loose函数被调用,我看到消息“beforeyield”打印出来。我等了三秒钟,但我从来没有看到“屈服后”消息

如果我把收益率取出来,函数的工作方式和我预期的一样,减去等待3秒的时间

这是关于Unity 4的。这段代码直接来自我认为是在Unity 3.5上创建的教程。我假设代码在Unity 3.5中起作用,因为网站上没有评论询问为什么收益率不起作用

我做错了什么蠢事?

你需要使用,就像这样:

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);

    // Change here.
    yield StartCoroutine(GUI.Lose());

    // Or use w/out a 'yield' to return immediately.
    //StartCoroutine(GUI.Lose());
}
您需要这样使用:

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);

    // Change here.
    yield StartCoroutine(GUI.Lose());

    // Or use w/out a 'yield' to return immediately.
    //StartCoroutine(GUI.Lose());
}

您也可以考虑在丢失函数上使用简单的调用。< /P>

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);
    Invoke("YouLose", 3.0f);
}

function YouLose()
{
    GUI.Lose();
}

您也可以考虑在丢失函数上使用简单的调用。< /P>

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);
    Invoke("YouLose", 3.0f);
}

function YouLose()
{
    GUI.Lose();
}

谢谢你,这让我有足够的时间来完成它。我将yield start例程(GUI.Lose())放在销毁之前。在此之前,我将renderer.enabled设置为false;所以我的游戏对象会隐藏起来。Lose()函数完成,然后我的游戏对象被销毁。说明:因为使用yield语句可以在任何时候暂停协同程序的执行。谢谢,这让我离完成它足够近了。我将yield start例程(GUI.Lose())放在销毁之前。在此之前,我将renderer.enabled设置为false;所以我的游戏对象会隐藏起来。Lose()函数完成后,我的游戏对象将被销毁。说明:因为使用yield语句可以随时暂停协同程序的执行。