Hi, Can someone tell me the difference between public, private, and static variables? When should they be used. I actually believe i understand static vars. But not the use of public and private.
Answer by Mike 3
Mar 21, 2011 at 05:13 PM
Public members are accessible from other classes
Private members are accessible only from within the class itself
Static members belong to the type/class instead of an instance - i.e. there is only one of them shared between all instances of a class
You can also use protected and internal - protected members are available from inheriting classes, internal works for other classes in the same assembly. These two probably aren't as interesting for you than the others though
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
No one has followed this question yet.
How faster is private variables over public variables?
Explanation on how to use functions, variables from other files
call variable from string?
Using [SerializeField] vs public
My static variable changes, but other scripts don't notice.