Need help with how my script needs to be attached

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) {

			Debug.Log("Current targetCounter " + targetCounter);

    void Die() {

    void changeColor() {
		gameObject.GetComponent<Renderer>().material.color =;



Any help is appreciated! Thank you <3

The targetCounter needs to be static, ie:

private static int targetCounter;

This would make it specific to the type, instead of the object.

static (C# Reference)

Then you only need to create a special object, lika a “GameController” with a tag for eaxmple “Controller” with a script for example “GameRecords”, with the targetcounter variable,

And make all cubes go look for that Object, get its script, and increase its variable. And only need to replace the


for something like


So, you find an object in the scene with the tag “Controller”, acces it script called “GameRecords” and increased by 1 its variable.

As all cubes are acessing the same script, that variable will be increased every time a cube executes the code.