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.
$$anonymous$$ake a GameController and declare "count" veriable in GameController Script. Now when you need to use count veriable you must access this through following code
GameController.SharedInstance().count++;
But now whenever I use it to modify the value it says
Assets/Scripts/PlayerController.cs(22,50): error CS1525: Unexpected symbol `='
On both scripts.
But I need to assign the variable to start at zero. Later in my script it adds to it. I want to able to assign a value to it, how do I do so? Here is the bit of code where I add to the value, how do I do so?
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
Format your code. Edit your posts, highlight the code and click the 101010 button.
Answer by usef155 · Oct 18, 2015 at 09:24 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612041336im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a global variable in Unity? 5 Answers
Reassigning variable inside OnCollisionEvent does not change variable inside the Update method 0 Answers
Can you make a global reference to a rigid body? 0 Answers
Generate a variable for each object that collides 2 Answers
C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer