- Home /
Whats the difference between public and static variable?
As question says whats the difference?
Answer by Wibbs 1 · Apr 08, 2011 at 09:59 PM
static - the same variable is shared by all instances of the class that are created, and can be private, protected or public.
public - a seperate variable for each instance of the class, and it is visible and accessible from outside of the class.
Very rapid and useful response thank you very much :-)
How would you access static variables from another script? I've tried scriptName.variable
but it doesn't work.
@jellyberg: Is your static variable public? This answer might be a bit misleading. The OP compares a public variable to a static variable which makes no sense since static and public are two different things.
static means there's only one (static) version of a variable which belong to the class and not to a specific instance.
public is an access modifier which controls the visibility of members.
To access a variable from outside a class the variable needs to be public.
@Bunny83 Ah right, so I would declare my variable with public and static before it - thanks, that works perfectly!
Answer by RaphDuBus · Apr 08, 2011 at 10:12 PM
As i undertand it :
- a public variable will be accessible by other scripts
- a private variable will not be accessible by other scripts
public & private var are unique for a script instance.
- a static variable will be shared betwenn all the instances of your script.
For more details : http://answers.unity3d.com/questions/47565/public-private-and-static-variables-in-js
Your answer
Follow this Question
Related Questions
Public variable not showing in Inspector(Solved) 1 Answer
How to make variable accessible by other scripts but not visible in editor? 1 Answer
GUI.DrawTexture Error & Static Variables!! 2 Answers
[C#] Is there a way to call static variables directly, independent from parent class? 3 Answers
Help With C# Static Boolean! 2 Answers