Health Script with GUITexture

I’m having a problem with my health script in that when one of my spiders die, they all die. At the same time, I’m trying to have a simple GUITexture floating above them to be somewhat representative of how much health they have left. Each spider is supposed to take two shots to kill.

var HB : GUITexture;

    function Update()
    {	
    	if(HealthBar.SHealth<1)
    	{
    		Destroy(gameObject);
    	}
    }

This script is on the spider with the spider having the GUITexture as a child in the hierarchy. The GUITexture then has a script of its own

var target : Transform; 
var gui : GUITexture;
static var SHealth = 2;

function Update () 
{ 
	if(target)
	{
		var wantedPos = Camera.main.WorldToViewportPoint (target.position); 
		transform.position = wantedPos;
		if(SHealth==1)
		{
			gui.pixelInset.width = 64;
		}

	}
}

The target of which is the parent spider, and the GUITexture variable is itself. The bullets are what cause the SHealth to drop with

function OnTriggerEnter(other:Collider)
{
    if(other.gameObject.CompareTag("Enemy"))
    {
        Destroy(other.gameObject);
    }
	if(other.gameObject.CompareTag("Spider"))
    {
		HealthBar.SHealth=HealthBar.SHealth-1;
    }
}

The first one is an enemy that’s working to a further extent then the spider, although it flies so the GUITexture seems to lag behind its movements somewhat. The first enemy does not however have health, like the spider.

Edit:I tried to post this in a comment, but it didn’t quite work out :stuck_out_tongue: Below is my attempt to condense the script above into a single script, but not my spiders disappear on play.

var target : Transform; 
var gui : GUITexture;
var SHealth = 2;

function Update () 
{ 
	if(target)
	{
		var wantedPos = Camera.main.WorldToViewportPoint (target.position); 
		transform.position = wantedPos;
		if(SHealth==1)
		{
			gui.pixelInset.width = 64;
		}
		if(SHealth==0)
		{
			Score_Player.Score=Score_Player.Score+5;
			Destroy(gameObject);
			if(Score_Player.Score>=100)
			{
				Application.LoadLevel(4);
			}
		}
	}
}
function OnTriggerEnter(other:Collider)
{
	if(other.gameObject.CompareTag("Bullet"))
    {
		SHealth=SHealth-1;
    }
}

The problem is SHealth is a static var. A static var is created only once, and all scripts share it. That’s why all spider die at the same time: all of them “see” the same SHealth.

1- You should declare SHealth as a non-static var:

  var gui : GUITexture;
  var SHealth = 2;

2- In the spider script, find the child GUITexture, then the health bar script and save it in a variable. NOTE: Replace “healtBar” with the name of your GUITexture, and HealthBarScript with the real name of the script (without quotes or extension):

// declare a var typed with the name of your script
private var hBar: HealthBarScript; // this is the name of your script

function Start(){
// find the script in the child GUITexture
  hBar = transform.Find("healthBar").gameObject.GetComponent(HealthBarScript);
}

Afterwards, you can use the SHealth variable anywhere in the spider script this way:

  hBar.SHealth = hBar.SHealth-1; // decrease spider life