I have two game objects in my game and when one object is shot the counter information but when I have two objects in my game it increments their individual counters. I.E
I believe there is a problem on how I am attaching the script. I want one counter for all the gameobjects being shot. Here is my code.
sing UnityEngine;
using UnityEngine.UI;
public class Target : MonoBehaviour {
public float health = 10f;
public int targetCounter;
public int arraySize;
public bool allTargetsHit = false;
//public GameObject cube;
private void Start()
{
}
private void Update()
{
}
public void takeDamage(float amount) {
health -= amount;
if (health <= 0) {
//Die();
changeColor();
targetCounter++;
//counter++;
Debug.Log("Current targetCounter " + targetCounter);
}
}
void Die() {
Destroy(gameObject);
}
void changeColor() {
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
}
Any help is appreciated! Thank you <3