- Home /
Better way for a timer to load a level?
Currently I'm using waitforseconds to delay my level loading controlled by a if statement if the score is a certain score per level, but what is happening is the coroutine is getting called like 10 or more times before that statement is false. I was thinking i could just replace waitforseconds with invoke, then do all i need to do to load the level in those functions but wouldn't those get called like 10 or more times too if they are still in the update function? How would i go about getting a delay for the level to load while constantly checking the score and then only calling a invoke once or waitforseconds once? here is my current code. Thanks.
public static ScoreBoard global;
public static float Score = 0f;
public static float Lives = 3f;
public static bool lvlChange = false;
public static float Timer = 0;
public static bool countdown = false;
public GameObject spawnHolder;
public static bool Respawning = false;
public static bool wonGame = false;
//public static bool paddleDieLvlChange = false;
// Use this for initialization
void Start () {
//global=this;
}
// Update is called once per frame
void Update () {
if (Application.loadedLevelName == "Level1" && Score >= 280) // should be 330
{
StartCoroutine(IntermissionL1());
Score = 0f;
}
if (Application.loadedLevelName == "Level2" && Score >= 360) // should be 360
{
StartCoroutine(IntermissionL2());
Score = 0f;
}
if (Application.loadedLevelName == "Level3" && Score >= 370) // should be 440
{
StartCoroutine(IntermissionL3());
Score = 0f;
}
if (Application.loadedLevelName == "Level4" && Score >= 430) // should be 430
{
StartCoroutine(IntermissionL4());
Score = 0f;
}
if (Application.loadedLevelName == "Level5" && Score >= 530) // should be 530
{
StartCoroutine(IntermissionL5());
Score = 0f;
}
if (Application.loadedLevelName == "Level6" && Score >= 470) // should be 470
{
StartCoroutine(IntermissionL6());
Score = 0f;
}
if (Application.loadedLevelName == "Level7" && Score >= 280) // should be 280
{
StartCoroutine(IntermissionL7());
Score = 0f;
}
if (Application.loadedLevelName == "Level8" && Score >= 340) // should be 340
{
StartCoroutine(IntermissionL8());
Score = 0f;
}
if (Application.loadedLevelName == "YouWon")
{
StartCoroutine(YouWon());
wonGame = true;
Score = 0f;
}
if (Lives == 0f)
{
Application.LoadLevel("GameOver");
Score = 0f;
Lives = 3f;
}
if (Ball_Movement.playerDead == true)
{
StartCoroutine(Countdown());
}
}
// Makes a GUI from the variable score which needs to be tied to when a block dies.
void OnGUI()
{
GUI.Box(new Rect(10, 430, 100, 20), "" + Score);
GUI.Box(new Rect(350, 430, 100, 20), "" + Lives);
if (countdown)
{
GUI.Box(new Rect(180, 400, 100, 20), "Respawning!!");
}
if (lvlChange)
{
GUI.Box(new Rect(40, 400, 500, 20), "You Won! Changing levels in 5 Seconds...");
}
if (wonGame)
{
GUI.Box(new Rect(40, 300, 300, 20), "You Beat the Whole Game WOOHOO!!!...");
}
}
private IEnumerator Countdown()
{
Ball_Movement.playerDead = false;
countdown = true;
yield return new WaitForSeconds(5f);
countdown = false;
RespawnPlayerAndBall();
}
private IEnumerator IntermissionL1()
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(5f);
Application.LoadLevel("Level2");
lvlChange = false;
}
private IEnumerator IntermissionL2()
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(5f);
Application.LoadLevel("Level3");
lvlChange = false;
}
private IEnumerator IntermissionL3()
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(5f);
Application.LoadLevel("Level4");
lvlChange = false;
}
private IEnumerator IntermissionL4()
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(5f);
//Ball_Movement.playerDead = false;
Application.LoadLevel("Level5");
lvlChange = false;
}
private IEnumerator IntermissionL5()
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(5f);
//Ball_Movement.playerDead = false;
Application.LoadLevel("Level6");
lvlChange = false;
}
private IEnumerator IntermissionL6()
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(5f);
//Ball_Movement.playerDead = false;
Application.LoadLevel("Level7");
lvlChange = false;
}
private IEnumerator IntermissionL7()
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(5f);
//Ball_Movement.playerDead = false;
Application.LoadLevel("Level8");
lvlChange = false;
}
private IEnumerator IntermissionL8()
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(5f);
//Ball_Movement.playerDead = false;
Application.LoadLevel("YouWon");
lvlChange = false;
}
private IEnumerator YouWon()
{
wonGame = true;
yield return new WaitForSeconds(7f);
Application.LoadLevel("Credits");
wonGame = false;
}
void RespawnPlayerAndBall()
{
GameObject paddleSpawn = (GameObject)Instantiate(spawnHolder, new Vector3(0, 1, -15), transform.rotation);
Respawning=true;
}
}
Answer by Bunny83 · Jun 15, 2012 at 09:05 AM
You could (or should) use parameters instead of 8 equal coroutines. Your Intermission coroutine should look more like this:
private IEnumerator Intermission(string aLevel, float aDelay)
{
Destroy(PaddleMove.Instance.gameObject);
Destroy(Ball_Movement.instance.gameObject);
lvlChange = true;
yield return new WaitForSeconds(aDelay);
Score = 0.0f;
Application.LoadLevel(aLevel);
lvlChange = false;
}
Also you have already your lvlChange variable which could be used in Update to not call the end levelchecks when a level change is already triggered.
Anyway, i would do it like this:
public string[] levels = new string[]
{
"Level1",
"Level2",
"Level3",
"Level4",
"Level5",
"Level6",
"Level7",
"Level8",
"YouWon"
}
public float[] levelScores = new float[]
{
280.0f, // level 1
360.0f, // level 2
370.0f, // level 3
430.0f, // level 4
530.0f, // level 5
470.0f, // level 6
280.0f, // level 7
340.0f, // level 8
0.0f // end
}
public static int currentLevel = 0;
void Start()
{
StartCoroutine(WinCheck());
}
IEnumerator WinCheck()
{
while(true)
{
if (currentLevel < levels.Length-1)
{
if (Score >= levelScores[currentLevel])
{
currentLevel++;
yield return Startcoroutine(Intermission(levels[currentLevel], 5.0f));
}
}
else
{
yield return StartCoroutine(YouWon());
}
yield return null;
}
}
I'm too new at program$$anonymous$$g to read and fully understand your example. But as is it loads the next level immediately. I tried launching it with lvlChange in a if statement in update but it says array index out of range, that way.
Sorry, got the limit wrong since i increment first. Fixed it ;)
I will add some explanations....
Here some explanations:
IEnumerator WinCheck()
{
while(true)
{
// ...
yield return null;
}
}
This is actually something similar to Update, but since it's a coroutine we can also interrupt it at anytime. This little piece of code will run every frame. yield return null will yield until the next frame, therefore it's executed once a frame like Update.
if (currentLevel < levels.Length-1)
{
// we still have levels to go
}
else
{
yield return StartCoroutine(YouWon());
}
This will check if there are still levels left. If we reached the last one (the won level) we execute the YouWon coroutine. Note that when you yield return another coroutine like i did here, the outer coroutine will wait until the inner coroutine has finished. So WinCheck will be suspended until YouWon() or Intermission() is finished.
if (Score >= levelScores[currentLevel])
{
currentLevel++;
yield return Startcoroutine(Intermission(levels[currentLevel], 5.0f));
}
This checks if the player reached the required score for the current level. If the condition is true we just increment the current level and call our intermission coroutine which will wait 5 seconds and then load the new current level.
If you are more comfortable with your Update "construct" ;) You would have to do something like that:
void Update()
{
if( !lvlChange ) // If we aren't changing the level at the moment ...
{
// ... check if we should
if (Application.loadedLevelName == "Level1" && Score >= 280)
{
StartCoroutine(Intermission("Level2", 5.0f));
Score = 0f;
}
else if //....
}
}
Your answer
Follow this Question
Related Questions
How do I put a delay in this script ? 1 Answer
C# simple delay execution without coroutine? 2 Answers
Wait n-seconds before loading level 1 Answer
Score and Level Loading 1 Answer
LoadLevelAdditive delay 2 Answers