Can one use of a static variable, change it globaly?

Hey.
Well, lets say i write a static variable called… um… jiggy_wiggy, and i use this static variable in 2 scripts.

If i change jiggy_wiggy’s value in script 1, will the value change in script 2, or will it just change in script 1?

I know its confusing… sry.

jiggy_wiggy is reference to the same point in the 2 script…So yes the value will change for the 2 script. The best way to learn programming is to do some test. Create 2 different script with a static variable, change that variable in the other script and Debug.Log the value of the variable in the other script.

[EDIT] Answer not very accurate, see comments below [/EDIT]

I wasn’t enough accurate in my answer thanks to @aldonaletto:

@aldonaletto comment:
“NOTE: If you have a script “Script1.js” with a static variable jiggy_wiggy declared and another script “Script2.js” with another static variable jiggy_wiggy, they aren’t the same! Inside Script1 you can read its jiggy_wiggy variable directly, but Script2’s variable must be accessed as Script2.jiggy_wiggy. Conversely, inside Script2 you can read its jiggy_wiggy directly, and must access Script1 variable as Script1.jiggy_wiggy. In any other script you must specify the “class name” (Script1 or Script2) in order to access the desired jiggy_wiggy.”

Yes, sir. A static variable is essentially a “class specific” global variable, in that only a SINGLE variable is created, and is accessible to each instance.

In fact, a “public static” variable is 100% identical to a global variable. In case this makes you wonder why a person would use one instead of a global, it’s for the sake of OOP. I use a public static variable in my EnemySpawner class:

public static int EnemyCount;

That way, my spawners can keep a running tally of the enemies in the area, but it’s still accessible to my enemies (allowing the enemy object to decrement the value upon death).