- Home /
Yield Never Works
Hello,I've been coding for a bit of time but I have never been able to get yield to work for me. It doesn't work. This is one of the scripts that needs to yield.
#pragma strict
var VictoryTheme : AudioClip;
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Player")
{
GameMaster.StopAllAudio();
AudioSource.PlayClipAtPoint(VictoryTheme, transform.position);
Time.timeScale = 0;
yield WaitForSeconds(3.5f);
Time.timeScale = 1;
GameMaster.CreateMusic = true;
GameMaster.MusicManager.GetComponent.<AudioSource>().enabled = false;
Application.LoadLevel ("MainMenuPorted");
}
}
The script happens when the player collects and item. It ends the level and sends them back to the menu. I want it to play a victory jingle and then load the next level, but it just yields forever and never ends. Thank you in advance.
Edit: I moved it to a separate function and I called it by start coroutine. But it works better now but now it just skips over the actual yield. Or in other words, it works but it doesn't yield. Here is the new code.
#pragma strict
var VictoryTheme : AudioClip;
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Player")
{
StartCoroutine("Stop");
Debug.Log("Finished the yield");
Time.timeScale = 1;
GameMaster.CreateMusic = true;
GameMaster.MusicManager.GetComponent.<AudioSource>().enabled = false;
Application.LoadLevel ("MainMenuPorted");
}
}
function Stop()
{
GameMaster.StopAllAudio();
AudioSource.PlayClipAtPoint(VictoryTheme, transform.position);
Time.timeScale = 0;
yield WaitForSeconds(VictoryTheme.length); // It skips past this line of code
}
thank you for the help.
yield is dependent on Time.timeScale. e.g. if timeScale is at 2x then the time alloted by yield statements gets used twice as fast so 1 second becomes 2 seconds. If you try to set Time.timeScale to 0x then the time alloted by yield statements gets used 0x as fast and will take forever.
Answer by Doozku · Jul 04, 2015 at 07:09 PM
I figured it out. maccabbe said that yield is linked to time.timescale. So basically I changed it from 0 to 0.0001 and multiplied the delay by 0.0001. Here is the new code.
#pragma strict
var VictoryTheme : AudioClip;
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Player")
{
GameMaster.StopAllAudio();
AudioSource.PlayClipAtPoint(VictoryTheme, transform.position);
Time.timeScale = 0.0001;
yield WaitForSeconds(VictoryTheme.length * 0.0001);
Debug.Log("Finished the yield");
Time.timeScale = 1;
GameMaster.CreateMusic = true;
GameMaster.MusicManager.GetComponent.<AudioSource>().enabled = false;
Application.LoadLevel ("MainMenuPorted");
}
}
So basically the error didn't have to do with functions or really with the yield.
thats an awful solution, create you own global time value for scaling what you need
private static float time = 1f;
public static void setTime(float value){
this.time = value;
}
//use this
public static float myCustomTime{
get{ return this.time;}
}
Answer by Dave-Carlile · Jul 03, 2015 at 09:22 PM
Yield only works in a coroutine. The Coroutine documentation has a decent example and description of how they work.
Answer by YoungDeveloper · Jul 03, 2015 at 09:15 PM
You can't use yield in trigger, it can't be an IEnumerator. Create separate function which will handle the yield and launch it.
It seems like you can use yield in trigger (in Javascript). I just tested the following code
#pragma strict
function OnTriggerEnter (info : Collider)
{
Debug.Log(0);
yield WaitForSeconds(3);
Debug.Log(1);
}
in a scene with 2 cubes each with istrigger set to true and a rigidbody that didn't use gravity and was kinematic and it worked as expected.
This is how it should be done http://answers.unity3d.com/questions/172055/help-with-yield-wait-for-seconds-.html
Personally i dont use unityscript as i find it extremely unreadable. You should specify the type in my opinion.
function $$anonymous$$yWaitFunction():IEnumerator{
}
New users dont understand the idea behind this just for this reason.
Answer by MCoburn · Jul 07, 2015 at 12:44 AM
Like what Dave Carlile said above, what I'd do is this:
When player achieves the goal (whatever that may be), then fire a coroutine that may be "LevelEnd". Tell the game not to accept any input, etc so player can't mess around while the coroutine is running. The coroutine does its job, saves score/life/etc to disk and then yields X seconds before finally saying "Unity, load this level".
Yield will not work in voids. C# will throw an error should you try to do so. Also, if you're using C#, you may need to do this:
return yield WaitForSeconds(1.5f);
I also highly recommend switching to C# if you haven't done so, UnityScript is moreso "legacy" and while it'll be around for some time longer, C# is in my books, cleaner and more powerful.