- Home /
Duplicate Question
Setting up health script. will not work,
I wanted to test a health script by placing it in a cube, the script will set Health to 100, and if the get hit by a bullet, they lose 50 of their health, and when they go to 0 health, they are destroyed, to test it, I placed it on a game object. set Trigger to true, and gave Bullet a tag, but it still won't destroy. did I make an error? It's a fairly simple script.
#pragma strict
var Health : int = 100;
var Bullet : GameObject;
function onBulletHit( hit : Collision ) {
if(hit.gameObject.tag == "Bullet"){
MaxHealth =- 50;
}
}
function onDeath() {
if(MaxHealth < 0)
Destroy(gameObject);
}
Problem is onBulletHit- and onDeath function is not connected programmatically to anything in the script we're seeing. Where does onBulletHit and onDeath get called?
Answer by South05 · May 11, 2013 at 06:27 AM
What save just said is right , you need some way of calling thoses
you'd probably want to change them to Update & OnTriggerEnter (since you're making the collider isTrigger) function OnTriggerEnter( hit : Collider ) { if(hit.gameObject.tag == "Bullet"){ MaxHealth =- 50; } } function Update() { if(MaxHealth < 0) Destroy(gameObject); }
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.html
you should go over this and see what suit your needs