2013-04-29 4 views
1

나는이 선수 오브젝트에 다음 코드 :수율 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 초를 기다린다. 그러나 "yield after"메시지를 보지 못한다.

출력량을 줄이면 함수는 3 초 동안 기다리지 않을 것으로 예상되므로 작동합니다.

이것은 Unity 4에 있습니다.이 코드는 Unity 3.5에서 만든 것으로 생각되는 튜토리얼에서 직접 가져온 것입니다. 왜 수익률이 작동하지 않는지 묻는 사이트에 대한 의견이 없기 때문에 Unity 3.5에서 코드가 작동했다고 가정합니다.

나는 어리석은 짓을하고 있습니까?

답변

4

당신은과 같이, StartCoroutine를 사용해야합니다 :

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()); 
} 
+0

고맙습니다. 마무리 해 주셔서 감사합니다. Destroy 전에 yieldCoroutine (GUI.Lose())을 배치했습니다. 그 전에는 renderer.enabled = false; 그래서 내 게임 객체는 숨길 것입니다. Lose() 함수가 완료되면 내 gameObject가 소멸됩니다. –

+0

설명 : coroutine의 실행이 yield 문을 사용하여 언제든지 일시 중지 될 수 있기 때문입니다. – Joetjah

0

는 또한 잃을 기능을 간단한 호출을 사용하는 것이 좋습니다.

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