Dying Problem!

I have a perfect script that works perfectly fine but when the health of the player reaches 0 it loads the level again but then the scene its loaded as if the player is already dead so the level is loaded and loaded again and again.

Script :

    var h00 : Texture2D;
var h10 : Texture2D;
var h20 : Texture2D;
var h30 : Texture2D;
var h40 : Texture2D;
var h50 : Texture2D;
var h60 : Texture2D;
var h70 : Texture2D;
var h80 : Texture2D;
var h90 : Texture2D;
var h100 : Texture2D;

static var HEALTH = 100;

function Update()
{
	var g_Health = gameObject.Find("g_Health");
	
	if(HEALTH > 100)
	{
		g_Health.guiTexture.texture = h100;
		return;
	}
	else if (HEALTH > 90)
	
	{
	    g_Health.guiTexture.texture = h90;
		return;
	}
	else if (HEALTH > 80)
	
	{
		g_Health.guiTexture.texture = h80;
		return;
	}
	else if (HEALTH > 60)
	
	{
		g_Health.guiTexture.texture = h60;
		return;
	}
	else if (HEALTH > 50)
	{
		g_Health.guiTexture.texture = h50;
		return;		
	}
	else if (HEALTH > 40)
	{
		g_Health.guiTexture.texture = h40;
		return;		
	}
	else if (HEALTH > 30)
	{
		g_Health.guiTexture.texture = h30;
		return;		
	}
	else if (HEALTH > 20)
	{
		g_Health.guiTexture.texture = h20;
		return;		
	}
	else if (HEALTH > 10)
	{
		g_Health.guiTexture.texture = h10;
		return;		

	}
	else if (HEALTH <= 0)
	{
		g_Health.guiTexture.texture = h00;
		Application.LoadLevel(1);
	}
}
  • Felipe

This line gets my attention:

    static var HEALTH = 100;

A static variable is shared throughout an entire program. Any time you change the value of HEALTH, you’re changing the value for every instance of that script, even if it’s attached to multiple characters, even if those characters are in different levels. Are you sure that’s what you want?

I see two obvious solutions:

  • If you’re not sure what a static variable is, you can probably just get rid of the static keyword.

  • If you’re using a static variable intentionally, you’ll probably want to reset HEALTH before loading a new level.