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.

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.

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#.

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