- Home /
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.
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 ins$$anonymous$$d 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).
Answer by Crazydadz · Nov 06, 2013 at 11:12 PM
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."
Crazydadz is right, that is the reason why it is called static, because it is global :). Don't forget to mark his answer, to many people forget to do it and then the answer never gets closed and he doesn't get credit for the correct answer :)
For more details, please see my answer here (the good coding habits part) - It's very important to get you head straight about statics - as they're well known as new-comers worst nightmare :) - Think twice before using a static variable "Do I really need it here?" 99.99 percent you won't, and yet could get away, with elegant solutions :)
how was this classified as an awnser by itself? o_O
well anyway, thanks for clarifying.
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.
Yet another reason why I dislike JS - It hides the concept of "class", behind "Script" - A Script is actually a class (technically speaking, a script is any file with any piece of code in it) yet in JS you don't see any indicators of that. i.e. when you first create a JS file, you're implicitly creating a class! But you don't see no class something : $$anonymous$$onoBehaviour
- but it's actually there, invisible!
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!
@WeeGee9000 if that confused you, let me explain it, in C#:
public abstract class Enemy : $$anonymous$$onoBehaviour
{
// stuff...
public static int counter = 0;
public Enemy() { counter++; }
}
public class Zombie : Enemy { }
public class Licker : Enemy { }
...
Enemy Enemy1 = new Zombie();
print (Enemy1.counter); // prints 1
Enemy Enemy2 = new Licker();
print (Enemy2.counter); // prints 2
The variable counter
is shared (global) between ALL Enemy
instances (regardless of what type it is) - So you might have a Zombie attached to some gameObject, a Licker attached to another, all those will have the same counter.
Now, if you come and do:
public class Bullet : $$anonymous$$onoBehaviour
{
public static int counter = 0;
public Person () { counter++; }
}
That counter has nothing to do with the Enemy counter, because they're different classes, they don't relate to each other.
So, different static variables in different "scripts" (classes) are totally separate things.
Answer by Aspirer · Nov 09, 2013 at 01:04 AM
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).
Thank you. This was helpful and it actually answered the question asked.