#accessing static variable in another script

1 messages · Page 1 of 1 (latest)

light rock
#

Hii, i have a score variable, its a public static int.
And i want to pass it into another script. But im a noob

Both scripts are attached to the same game object

hearty birch
#

Why are you accessing it through script

#

When you've made it static...

#

I don't think you understand what static means

long yoke
#

you dont need to pass it to another script becouse the other script can access the static score.
The way I explain static is Imagine you have 1 script called Cars and you put that Cars script into 3 objects. Each one of them has a seperate Variable called GassPrice. Then you can put a gass price into each object (example: 3, 2, 15). But if you want them to always have the same gass price no matter what and you want to be able to change it so every single one of them has it. You can set the GassPrice static and as soon as you change it every single one of objects will have the same variable.

So basicaly static makes a variable or function the same in each instant.

hearty birch
#

Furthermore why is static being used here anyway? I think your code design is flawed

long yoke
#

You can access the score like this

int score;
Void Awake()
{
score = CrystalScore.score;
}

light rock