- Home /
i'm beginner in unity ! my problem is i can't count the number of zombie killed ! this script ZombieHelath is attached to zombie , i'm using javascript !Please help
pragma strict
ar ZombieHealth : int = 100 ; function OnCollisionEnter (col : Collision) { if ( col.gameObject.tag == "Bullet") { ZombieHealth -= 25 ; } }
function Update () {
if ( ZombieHealth <= 0 ) { GetComponent.().Play("back_fall"); gameObject.GetComponent(ZombieAI).enabled = false ; gameObject.GetComponent(CharacterController).enabled = false; Dead () ; } } function Dead () {
yield WaitForSeconds (5) ; Destroy ( gameObject) ;
}
Answer by solidsnake530 · Aug 24, 2017 at 01:37 PM
I'm not much help, but you might get some help if the code was a bit easier to read so I've formatted it for you.
What I can see is that the zombies are dying but you're not counting them anywhere. Maybe put in a counter into the Update() function? I don't know JS but I'll try to help out, I've added a couple of lines in red:
#pragma strict
var ZombieHealth : int = 100 ;
var DeadZombies : int = 0;
function OnCollisionEnter (col : Collision)
{
if ( col.gameObject.tag == "Bullet")
{
ZombieHealth -= 25 ;
}
}
function Update ()
{
if ( ZombieHealth <= 0 )
{
GetComponent.().Play("back_fall");
gameObject.GetComponent(ZombieAI).enabled = false ;
gameObject.GetComponent(CharacterController).enabled = false;
Dead () ;
DeadZombies +=1;
}
}
function Dead ()
{
yield WaitForSeconds (5) ; Destroy ( gameObject) ;
}
You could read DeadZombies as a public variable (unsure how this is done in JS) and have it displayed on a GUI object.
Your answer
Follow this Question
Related Questions
Manipulating Variables Over Multiple Scripts 0 Answers
Make a variable start at 10 and count down until 0 3 Answers
Reading variable from other script 1 Answer
What is typed variable? 2 Answers