Global Variable !

Hi everyone :slight_smile:
i have special question about global variables so i want explain it with an example:

i have a Creator script that creates cubes, this script has a Counter which is used for keep track of number of total cubes;

i have multiple machines that use the Creator script for creating cubes so when each machine create 2 cubes the Counter variable in each machine script is 2 while i want Counter variable do not start from 0 for each script.
and the final value of Counter be 4.

All i need is a global variable that do not resets when new object starts using it.

sample variable value through running multiple scripts:


machine1 counter=1

machine1 counter +=1

machine2 counter +=1

machine3 counter +=1

machine2 counter +=1

now counter should be 5 not with different value in each script

Most likely this is what youโ€™re looking for. With only difference that you can set static value just inside your Creator script without the need of creating custom class.

Yep like Dota-KAMIKADzE said you can use something like this
static int totalCounter = 0;

void AddCube() {

Each machine that ads a cube will increment totalCounter, but total counter is only one variable across all of machines that use this script