- Home /
Kill Counter Doesnt Work On Instantiated Objects!
Hello, thanks in advanced for any answers. So im making a game where I have creatures that spawn (using instantiate) and I have a kill counter, but the counter only counts to one, when I kill the first enemy, then it just stops counting. I think its because every copy starts counting again from zero. Here is the code that goes into my enemy (its also a health script):
using UnityEngine; using UnityEngine.UI;
public class health : MonoBehaviour { public float hitpoints = 100f; public Text countText; int count;
public void Start()
{
count = 0;
}
public void recievedamage(float amt)
{
hitpoints -= amt;
if (hitpoints <= 0)
{
PlusPoints();
Die();
}
}
void Die() {
Destroy(gameObject);
}
void PlusPoints()
{
count = count + 1;
countText.text = "Kills: " + count.ToString();
Debug.Log(count);
}
}
Answer by $$anonymous$$ · Jun 24, 2016 at 07:03 AM
I would suggest using two scrips, one for receiving damage and one for keeping score. The receive damage script is for the enemy which will tell the scoring when it is hit. So you will have one scoring script and many damage scripts. Keep the scoring alive and destroy the enemies after they have passed their score.
PS: it is receivedamage not recievedamage ;-)
Yep, this is correct.
@NitroGain you could see this tutorial for more info https://unity3d.com/learn/tutorials/projects/survival-shooter-tutorial/scoring-points
Thanks for the answer, it will probably work, still haven't tried it but it makes lots of sense :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Script not checking if a different script is active. 1 Answer
Trying to reset image settings on main camera smoothly 1 Answer