- Home /
Question by
Essexrookie · Feb 15, 2019 at 01:49 PM ·
c#tagscoremultiple
Trouble with just scoring 1 with multiple gameobjects collision
Hello, I have a random number between 1 - 9 of the same Enemy with tags spawning and a collider2D to detect the collisions of the tagged gameobjects but when collided it counts all gameobjects when I only need it to count 1 by 1 no matter the number of objects
private ScoreScript score
void Start()
{
score = GameObject.FindObjectOfType<ScoreScript>();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag(“Enemy”))
{
// This function just adds the texts int value intValue += 1;
score.Score();
}
}
Comment
Answer by bpaynom · Feb 15, 2019 at 02:55 PM
You can use a counter, increment it OnTriggerEnter2D, and decrement it OnTriggerExit2D. When an enemy fires OnTriggerEnter2D, you just call score.Score() if the counter is 1 (if you increment before the call) or 0 (if you increment after the call).
Your answer
![](https://koobas.hobune.stream/wayback/20220612191749im_/https://answers.unity.com/themes/thub/images/avi.jpg)