How to use global variable to make counter

I am trying to make a global variable in C# that is modified by two different scripts so that when a game object is collected it adds 1 to the current value of “count” (the name of the variable). For the variable definition I used this line of code.

public static int count;

I applied this to both scripts. The next bit is the collider code.

 void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.CompareTag ( "Pick Up"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
            SetCountText ();
        }
    }

Both of the game objects/scripts detect the cubes being counted but only one of them contributes to the variable “count”. They both have the exact same bit of code for the collider.

Okay, so I found out how. Here are some little snippets of code explaining the way I did it.

At the beginning of your code you need to create a public class with a name of your choice (I chose Global so I could remember what the class is for). Next you need to make a public static int followed by the name of the variable you want to make global. Below is what I used in my code to make my global variable by the name of “count”.

public class Global 
{
	public static int count = 0;
}

Now that we have created a global variable by the name of count with a default value of zero, we need to find out how to refer to that global variable, and how to edit it’s value.

Whenever you want to refer to your global variable you would now call it Global.count or whatever you decided to call yours. If your class was Example and your variable name was ExampleVar then you would refer to it as Example.Examplevar in your code.

And now, I am going to show my code where when my player collects/collides with an object it adds 1 to value of my global variable.

void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.CompareTag ( "Pick Up"))
        {
            other.gameObject.SetActive (false);
            Global.count = Global.count + 1;
        }
    }
}

As you can see I used and if state so that when the player collides it adds 1.