Health script doesnt work?

Hello! Im making a simple game and i wanna make a health system.

but why is my Health GUI doesnt Decrease?

Heres the code : HealthSystem

#pragma strict

function Start () {

}

function Update () {

}

 
function OnTriggerEnter (col : Collider)
{
	if(col.gameObject.name == "Sphere(Clone)")
	{
		PlayerHealth.PlayerHealth -= 1;
	}
}

When the AI shoots my GUI doesnt Decrease.
Is there any Mistake in my script?

Ok heres my health script :
PlayerHealth

//Health Textures
var health5 : Texture2D;
var health4 : Texture2D;
var health3 : Texture2D;
var health2 : Texture2D;
var health1 : Texture2D;

//Intergers
static var PlayerHealth = 5;

//Textures
var HealthGUI : GUITexture;

	switch (PlayerHealth)
	{
		case 5:
			HealthGUI.texture = health5;
		break;
		case 4:
			HealthGUI.texture = health4;
		break;
		case 3:
			HealthGUI.texture = health3;
		break;
		case 2:
			HealthGUI.texture = health2;
		break;
		case 1:
			HealthGUI.texture = health1;
		break;
	}

just looking quickly, you have named both your class and the variable with the same name. Could this be the problem? Try changing the name of the health variable, or even the format to camelCase, to indicate a difference between the script and the variable. Oh, and really you should typecast, it shows that you know what is going on, and in future make reading C# much easier.

static var playerHealth : int = 5;

therein lies the other problem. With using a static variable, it is not visible in the inspector. So for testing you should set up a Debug.Log before your switch-case, or write a GUI function to display the current value for playerHealth

Debug.Log( "playerHealth = " + playerHealth );
switch (PlayerHealth
 ....

or

function OnGUI()
{
    GUI.Box( Rect( 10, 10, 100, 30 ), "" + playerHealth.ToString() );
}

When you say Ok heres my health script : PlayerHealth , I hope you mean this is code taken from that script. Please tell me that your switch is indeed within a function, and not as displayed here.

I could go on and say if you used a built in array for your textures, then instead of a switch you could just simply assign the texture by using playerHealth as an index to that array, making the whole thing one line. But That’s not what you’ve done, so I won’t confuse the issue.

So :

  • please tell me your switch is really within a function
  • rename your playerHealth variable so it does not share the same name as your script (or anything else for that matter)
  • Debug your playerHealth so you can see if the value is actually being modified