- Home /
try level for 3x then on the 4th go to different level
hi! im a newbie here and im trying to create this game where in you got 3 tries and after that you return to the menu.
so after 1 try, i need to load the level again. and after 3 tries. i need to load the main level.
here's my code. it tries again but never goes back to the main menu.
var levelToLoad: String;
var levelToLoadgo: String;
var beep: AudioClip;
static var tryagain : int = 1;
if(tryagain!=3){
audio.PlayOneShot(beep);
yield new WaitForSeconds(0.05);
Application.LoadLevel(levelToLoad);
}
if(tryagain>=3){
audio.PlayOneShot(beep);
yield new WaitForSeconds(0.05);
Application.LoadLevel(levelToLoadgo);
tryagain = 1;
}
in another script that handles time i got
stagegameover.tryagain++;
at the time over mark.
Comment
Answer by Wolfram · Jan 11, 2013 at 02:00 PM
Depending on how often you call that script, you might run into a conflict where tryagain was already incremented, but the Application.LoadLevel wasn't executed yet, due to the yield.
Try rearranging your code blocks, and connect them with "else", to make sure that only one of them gets executed, not both:
if(tryagain>3){ // you initialize tryagain already to 1, so don't use ">="
// load main
...
}else{
// restart level
...
}