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 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;
}
}