Game freezes when i reload my level :(

When i try to reload my level with the function “Application.LoadLevel(Application.loadedLevel);” the game freezes :frowning:

The whole script:

static var health : float = 100;
var playerHealthMAX : float = 100.00;
var customGUISkin: GUISkin;
private var playerHealthBar : float;
var gameover : Texture;
private var horizontal : float = Screen.width;
private var vertical : float = Screen.height;
static var loadedLevelName : String;

function OnGUI ()
{
    playerHealthBar = health / playerHealthMAX;   
    if (health > 0)
    
    {
    
       
       GUI.Box(Rect(10, 10, 200, 20),"");
       GUI.skin = customGUISkin;
       GUI.Box(Rect(10, 10, (200 * (health / playerHealthMAX)), 20),""); 
       GUI.skin = null; 
    
    }

if (health >= playerHealthMAX){
health = playerHealthMAX;
}
if(health <= 0){
Application.LoadLevel(Application.loadedLevel);
}
}

There is nothing wrong with your script it must be your computer or a other script. Is your computer not responding?

No,the level is loading but i cant move and nothing happen,its like a single picture.I can click things in Unity and all it’s only the game.And yes,i have also build it and try it then. :frowning:

Try disabling all the other script in the scene and make it reload. Tell me if it freezes then.

It’s freezing again :((((

Make a new scene with a plane and a box in it. attach this script to the box

yield WaitForSeconds (2);
Application.LoadLevel(Application.loadedLevel);

Tell me if it freezes again.

Ok, i got it work now,but i dont know why ? :open_mouth:

new script:

static var health : float = 100;
var playerHealthMAX : float = 100.00;
var customGUISkin: GUISkin;
private var playerHealthBar : float;
var gameover : Texture;
private var horizontal : float = Screen.width;
private var vertical : float = Screen.height;
static var loadedLevelName : String;

function OnGUI ()
{
    playerHealthBar = health / playerHealthMAX;   
    if (health > 0)
    
    {
    
       
       GUI.Box(Rect(10, 10, 200, 20),"");
       GUI.skin = customGUISkin;
       GUI.Box(Rect(10, 10, (200 * (health / playerHealthMAX)), 20),""); 
       GUI.skin = null; 
    
    }

if (health >= playerHealthMAX){
health = playerHealthMAX;
}
if(health <= 0){
level = Application.loadedLevel;
Application.LoadLevel(level);
health += 100;
}
}

Awesome! For less strain in the game set the level var when the script starts.