Application.LoadLevel problem and Play again logic

There are 3 scenes in my game.

  1. MainMenu - there is a start game button, when click it, it goes to next scene.
  2. Loading - it loads the 3rd scene.
  3. Game - the game

In the Game, there is a script to control character lives.

here is the code:

function Update () 
{	
   switch(LIVES)
   {
   	   case 3:
   	       guiTexture.texture = health3;
   	       break;
   	       
   	   case 2:
   	       guiTexture.texture = health2;
   	       break;
   	       
   	   case 1:
   	       guiTexture.texture = health1;
   	       break;
   	       
   	   case 0:
   	       Application.LoadLevel ("MainMenu");
   	       break;
   }
   
   if(playerScore >= 100)
   {
   	   Application.LoadLevel ("MainMenu");
   }
}

Either Character got 100 score to win or lose 3 lives to die, it returns to MainMenu.

Then, I click on Play Game button in MainMenu, it goes to loading scene and Game scene.

However, it quickly returns back to MainMenu again.

So, how should I code the Application.LoadLevel properly?

In fact, I would like to build a scene called “PlayAgain”, after character dies, then it will go to that scene to ask whether user wants to play it again.

Is that logic correct? and what is the usual practice to code it?

What are you initializing the variable LIVES to?

static var LIVES : int = 3;

function OnCollisionEnter(hit : Collision)
{	
	if(hit.gameObject.tag == "Character")
	{
		PlayerControl.LIVES -= 1;	
	}
}

When the enemy hit character, it will reduce the LIVES by 1.

It is common for several collisions to happen in quick succession. What happens when the player gets hit? Does he move to a restart position or stay in the same place where the collision occurred?

What happens when the player gets hit? Does he move to a restart position or stay in the same place where the collision occurred?

ans: he moves to a restart position when the player gets hit

Maybe your lives and score variables are not reset to default when playing a second time. You could try setting these to default values in the start function of your game scene.

Pretty obvious whats going on here.

Static Var’s are not reset during a LoadLevel. you must re-initialize the lives counter so in that same script you could use

function Awake(){
    PlayerControl.LIVES = 3;
}

Great help! it is fixed!

Thanks a lot