- Home /
Script Not counting enemy kills.
I'm sorry if this question appears as double post, but i think my other question was not approved from the queue for some reason. Anyway, can you please help me here guys?
I have this script attached to my cannon:
pragma strict
var firePrefab: Transform; var deadEnemies: int = 0;
function OnTriggerEnter (other : Collider) { if(other.tag =="enemy") { Instantiate(firePrefab, gameObject.transform.position, Quaternion.identity); Destroy(gameObject); deadEnemies = deadEnemies+1; Destroy(other.gameObject); Debug.Log(deadEnemies); } else { Instantiate(firePrefab, gameObject.transform.position, Quaternion.identity); Destroy(gameObject); } }
function OnGUI () { // every GUI update GUI.Box (Rect ( 10,90,150,30), "Kills: " + deadEnemies); // create a text box with the health in there }
The script is then attached to my bullet prefab, and for the part of hiting the enemies and destroying them... It works. Now the two problems i'm facing are the fact that, first, the variable deadEnemies only updates once, at the first enemy i destroy, and the other problem is that i can't manage to make the count appear on the gui box, it was supposed to show the variable deadEnemies at the top, and it only appears for a few seconds, everytime i press the fire button. And it is not updated since always shows zero, in other words, it's not even giving me the same value as debug log. What am i doing wrong.. can you help me please? I have to deliever this assigment tommorow and this isn't working as i wanted to. Save me please. Thanks...
Answer by Doireth · Jul 22, 2012 at 08:29 AM
If the script is attached to the bullet prefab then the integer holding the dead enemies value is destroyed with the bullet. Attach the script to the object firing the bullet instead (i.e, the player) as it won't be destroyed.
Or you could make the deadenemies value static
static var deadEnemies : int = 0;
that way it would be global to all instances of the script and should remain (I think).
Your answer
Follow this Question
Related Questions
Display var on gui not working... 2 Answers
how can i destroy a cube when i press a gui button??? 1 Answer
Randomizate bullets help 2 Answers
kill script! 2 Answers
Count the Score 1 Answer