- Home /
A yield function for function Update?
I need to add a yield new WaitForSeconds(2); where the application loads the level...How will I do this? :)
var timer: float = 30;
var isFinishedLevel : boolean = false;
function Update()
{
if (!isFinishedLevel)
{
timer -= Time.deltaTime;
}
if (timer > 0)
{
guiText.text = timer.ToString("F0");
}
else
{
GameMaster.currentScore +=1;
if (GameMaster.currentScore > PlayerPrefs.GetInt("ScoreScript")){
PlayerPrefs.SetInt("ScoreScript", GameMaster.currentScore);
}
Application.LoadLevel("Level5");
}
}
Hmm...you already have a 30 second timer. Yielding for two more seconds would be the same thing as just starting the timer at 32.
Or do you want to load a new level then wait two seconds? Or wait 30 seconds, freeze the game for two seconds, then load?
Answer by robertbu · Mar 10, 2014 at 10:57 PM
'yield' cannot be used in Update(), so to get your new scene, pull the LoadLevel() into a new function:
function LoadNextLevel() {
yield WaitForSeconds(2);
Application.LoadLevel("Level5");
}
Then you would call LoadNextLevel() on line 22 instead of the Application.LoadLevel().
Note there as alternate, you could leave out the 'yield', and use Invoke() to run LoadNextLevel() in two seconds.
hello, I tried putting a new function and putting it there but it also didn't work. can anyone please change accordingly the complete script for me as I tried a lot of options and cant move on. Basically I would like to freeze the game for two seconds, then load the new level.
This is what I had in $$anonymous$$d. Note I added a 'loading' variable so that LoadNextLevel() only gets called once.
#pragma strict
var timer: float = 30;
var isFinishedLevel : boolean = false;
var loading = false;
function Update()
{
if (!isFinishedLevel)
{
timer -= Time.deltaTime;
}
if (timer > 0)
{
guiText.text = timer.ToString("F0");
}
else
{
Game$$anonymous$$aster.currentScore +=1;
if (Game$$anonymous$$aster.currentScore > PlayerPrefs.GetInt("ScoreScript")){
PlayerPrefs.SetInt("ScoreScript", Game$$anonymous$$aster.currentScore);
}
if (!loading)
LoadNextLevel();
}
}
function LoadNextLevel() {
loading = true;
yield WaitForSeconds(2);
Application.LoadLevel("Level5");
}
Your answer
Follow this Question
Related Questions
How do I create a GUI on collision detection? 1 Answer
Yield function goes slow sometimes/differently? 2 Answers
GUI.Window function with variables? 3 Answers
How can I use a yeild during an udate? 1 Answer
Im a bit confused on a simple script 1 Answer