Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by straydogstrut · May 24, 2010 at 11:06 PM · variablepublicdeclaration

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
18
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image straydogstrut · May 24, 2010 at 11:36 PM 0
Share

Thanks Duck, good to know.

avatar image NewfieJoe · May 20, 2011 at 09:51 PM 0
Share

Don't you HAVE to specify public or private when using pragma strict?

avatar image Eric5h5 · May 20, 2011 at 10:34 PM 0
Share

@NewfieJoe: No, public/private has nothing to do with dynamic typing or anything.

avatar image Rafes · Mar 18, 2014 at 05:47 PM 1
Share

[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

avatar image Eric5h5 · Mar 18, 2014 at 06:01 PM 0
Share

The default is private. Read the link again.

avatar image kitanganday Eric5h5 · Oct 08, 2017 at 09:40 PM 0
Share

Nope @Rafes is right. Proof is here and here

avatar image Eric5h5 kitanganday · Oct 08, 2017 at 11:32 PM 0
Share

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

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I declare a variable inside a function which has instance scope? 1 Answer

Why can't I to declare public the variable "Clave"? 1 Answer

Is there something more "public" than public? 3 Answers

Public Variable not updating 1 Answer

Enum-Variable with float 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges