irregular decrease of the lives in health control

hi guys i have a health control script thats have some problem,when i play the game its working fine by minus 1 lives but when i replay the game its minus 3 lives or even 6 lives.
i do not know why i suspect is because i have 2 different damage style that make the health control script crash, one is fallout the other one is bullet.

here my health control script:

var health3 : Texture2D;
var health2 : Texture2D;
var health1 : Texture2D;

var player : Transform;

static var Lives = 3;
private var playerInfo : ThirdPersonStatus;

// Cache link to player's state management script for later use.

function Awake()
{
	playerInfo = FindObjectOfType(ThirdPersonStatus);
	if (!playerInfo)
		Debug.Log("No link to player's state manager.");
}

function Update()
{
 print("Lives: "+Lives+" Hits: "+HITS);
		switch(Lives)
		{
				case 3: 
				guiTexture.texture = health3;
				break;
				case 2:
				guiTexture.texture = health2;
				break;
				case 1:
				guiTexture.texture = health1;
				break;
				case 0:
				Application.LoadLevel("3");
				break;
			}

and here my thirpersonststus:

function LateUpdate()
{
			if(dead)
			{
						transform.position = Vector3(639,39,427);
						gameObject.Find("Main Camera").transform.position = Vector3(639,39,-427);
						dead = false;
			}
			}

function OnControllerColliderHit(hit : ControllerColliderHit)
{
		if(hit.gameObject.tag == "fallout")
		{
		dead = true;
		HealthControl.Lives -= 1;
		}
		
		else if(hit.gameObject.tag == "bomgy 1")
		{
		dead = true;
		HealthControl.Lives -= 1;
		
		}
		
		if(hit.gameObject.tag == "spike")
		{
		dead = true;
		HealthControl.Lives -=1;
		}
    }

please fix your code formatting, there is a code block button ( icon is 0s and 1s )

1 Answer

1

If you’re replaying the game by using the “application.loadlevel” then the lives wont get reset to 3 or whatever your default value is.

Because.

you’re using a static variable as the lives, Static variables dont get reset between level loads because thats the way they work (there’s some longer explanation as to why, but i really cant remember it >_<), what you can do in the “start function” of this script, maybe “awake” even, is to set lives to their default value, that should fix it.

try writing this in the beginning of the script where your health variable is declared: function Start(){ Lives = 3; }