- Home /
What is the difference between 'public var' and 'var' when declaring variables?
This was raised in the comments of another question but it was suggested I make it a standalone question.
Unity is my first proper taste of Javascript so i'm still a bit rusty on the fundamentals. Before Unity I used Director MX 2004 so I understand the difference between private and global variables. In Unity, i've been using member, private and global variables as defined by the scripting docs with no problems. Bear with me while I outline the differences for others (as quoted in the docs):
member variables
Any variable defined outside of any function defines a member variable. The variables are accessible through the inspector inside Unity. Any value stored in a member variable is also automatically saved with the project.
var memberVariable : int = 0.0;
private variables
Private member variables are useful for storing state that should not be visible outside the script. Private member variables are not saved to disk and are not editable in the inspector.
private var myHiddenVariable : int = 0.0;
global variables
You can also create global variables using the static keyword.
static var someGlobal = 5;
To access it from another script you need to use the name of the script followed by a dot and the global variable name.
TheScriptName.someGlobal = 10;
However, i've recently started coming across the public keyword but little mention of it except in the Unify list of keywords, but the description doesn't really help me since I suspect I still have much to learn about JS itself, never mind UnityScript.
Since this seems like a useful bit of info for anyone, could someone please explain the difference between using
public var myVariable : int = 0.0;
and
var myVariable : int = 0.0;
to expose a variable?
Many thanks.
Answer by duck · May 24, 2010 at 11:18 PM
There's no difference.
In Unity's Javascript, variables and functions are public by default. (This is in contrast to C#, where they're private by default). This means if you omit the public/private specifier, they will be implicitly public.
Therefore whether you explicitly include the word 'public' or not, comes down to a matter of programming style or aesthetic.
Don't you HAVE to specify public or private when using pragma strict?
@NewfieJoe: No, public/private has nothing to do with dynamic typing or anything.
[Edited] Before I said C# default is internal
and linked http://stackoverflow.com/questions/2917495/method-without-access-modifier, which DOES go on to explain that this is only partially true. Classes which are not nested are internal, members and nested classes are private. Here are the .Net docs:
Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.
Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.
I honestly can't recall the last time I didn't type the access modifier. It is annoying to me that the languages even make them optional. I feel the same annoyance that UnityScript makes classes ambiguous. /rant-off
Er, no. I don't think you understand what this question is talking about. Directly from $$anonymous$$icrosoft, who would probably know: "The access level for class members and struct members, including nested classes and structs, is private by default."
Answer by ThePunisher · Nov 17, 2013 at 10:53 PM
There isn't a difference in Javascript. This is why.
By declaring the access modifier you are forcing the variable to be what you specify:
public: Visible to everyone.
protected: visible to children.
private: Not visible to anybody outside the defining class.
When you don't define a access modifier then it will default to one of the three values, depending on the language. This default behavior can differ from language to language therefore it is advisable to simply specify the access modifier. For instance, it will default to private in C#.
Answer by edragongaming · Mar 18, 2014 at 07:20 PM
I know at least in C# that adding public variables lets you use it in more than one script so you don't have to recreate the variable every time you want to use it. private variables are for if you only want to use it within that script. I think this transfers over to javascript too