Application.LoadLevel not working

Hello everyone,

I’m thinking this one should be simple, and I just am not seeing the solution. In fact, I had it working last night, but tried to get too creative and screwed things up. I’ve gotten back to “working,” but not quite right.

Here it is: In my player health script (js) I have a condition to throw the player to the “Game Over” scene upon hp reaching 0 using "Application.LoadLevel(x). (Line 47) The problem is, it’s not doing anything. I’m not getting an error, but nothing’s happening. Instead, my player healthregen just kicks in and keeps adding hp. My entire newbness in coding is shining through here, because I know the solution has got to be stupid simple. Thanks to anyone who can help, and God bless.

 var curHealth : int = 35;
    var maxHealth : int = 35;
         
    var healthtext : GUIText;
         
    var curXp : int = 0;     
    var maxXp : int = 100;     
    
    var xptext : GUIText;
    
    var level : int = 1;
    
    function Start () {
         
         
    healthRegen();
         
    }
         
         
         
         
         
    function Update () {
         
         
    healthtext.text = curHealth + "hp / " + maxHealth;
    
    xptext.text = "Level " + level + " XP " + curXp + " / " + maxXp;     
    
    
    if(curXp == maxXp) {
    
    
    levelUpSystem();
    
    
    }
    
    
    
    if(curHealth < 0 ) {
    
    curHealth = 0;
    
    Application.Loadlevel(2); 
    
    
    
    }
         
         
    if(curHealth > maxHealth) {
         
         
    curHealth = maxHealth;
         
         
    }
         
         
         
    if(Input.GetKeyDown("e")) {
         
    curHealth -= 10;
         
    }
         
    if(Input.GetKeyDown("r")) {
     
    curXp += 10;     
              
    }
    
    
    
    
    }
    
    
    
    
    function healthRegen () {
         
         
    for(i=1;i>0;i++) {
         
         
         
    yield WaitForSeconds(5.0);
         
    if(curHealth < maxHealth) {
         
    curHealth++;
         
    }
         
         
    }
         
         
    }
    
    function levelUpSystem () {
    
    
    
    curXp = 0;
    maxXp = maxXp + 50;
    
    
    
    maxHealth += 15;
    level++;
    
    
    
    
    
    
    }

The problem most likely exists in the check to see if the current health is less than zero. The way you have it written the health must be below zero before the level will load, if you want the load to take place exactly at zero as well just change the conditional to this:

if(curHealth <= 0 ) 
{ 
  curHealth = 0; 
  Application.Loadlevel(2); 
}

EDIT: Just for good measure I loaded a project in Unity with 3 scenes and got this to work. Here is the full code implementation with the change to the conditional listed above

var curHealth : int = 35;
var maxHealth : int = 35;
 
var healthtext : GUIText;
 
var curXp : int = 0;     
var maxXp : int = 100;     
 
var xptext : GUIText;
 
var level : int = 1;
 
function Start () 
{
	healthRegen(); 
}
 
 
function Update () 
{
	healthtext.text = curHealth + "hp / " + maxHealth;
 		xptext.text = "Level " + level + " XP " + curXp + " / " + maxXp;
 		
 		if(curXp == maxXp) 
	{ 
		levelUpSystem(); 
	    }
 		
 		if(curHealth <= 0 ) 
 		{
 		     curHealth = 0;
		     Application.LoadLevel(2); 
	    }
	
	if(curHealth > maxHealth) 
	{ 
		curHealth = maxHealth;
	}
 		
 		if(Input.GetKeyDown("e")) 
 		{
 			curHealth -= 10;
 		}
 		
 		if(Input.GetKeyDown("r"))
 		{
 			curXp += 10;
 		}
	}    
 
function healthRegen () 
{
 		for(i=1;i>0;i++) 
 		{
 			yield WaitForSeconds(5.0);
 			
 			if(curHealth < maxHealth) 
 			{
 				curHealth++;
 			} 
	    } 
}
 
function levelUpSystem () 
{
	curXp = 0;
	maxXp = maxXp + 50;
	
	maxHealth += 15;
	level++; 
}