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

120621-screen-shot-2018-07-11-at-70835-pm.png

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

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

targetCounter++;

for something like

GameObject.FindObjectWithTag("Controller").GetComponent<GameRecords>().targetCounter++;

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.

Bye!