Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Anxo · Oct 21, 2015 at 03:37 PM · c#variablespublic variable

Variable declaration. Public vs indirect public?

So when I create a public variable that needs to be assigned by another class I do it like so.

 public string variableA = "";

But when I look at some other people's code, I often see this indirect way to declare a public variable.

 private string _variableA = "";
 
 public string variableA {
     get{
            return _variableA;
     }
     set{
           _variableA value;
      }
 }

Now I often use private variables that I prefix with the underscore if only that class uses them, but why declare public variables this way?

Thank you.

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
1
Best Answer

Answer by Dave-Carlile · Oct 21, 2015 at 03:58 PM

These are called "properties" and it's a way to better hide your internal object state from outside influence. For base types like numbers and strings it's probably not a huge deal to just expose the variable if the value doesn't matter beyond the data type. But if you need to only accept certain values properties are a good way to validate the value while still treating it like a variable...

For example, when a property is being set you can validate the range:

 public float SomeRangeValue
 {
     get { return _rangeValue; }
     set
     {
         if (value < minRange || value > maxRange)
         {
               throw new ArgumentOutOfRangeException(...);
         }
         else
         {
              _rangeValue = value;
         }
     }
  }

And you can do calculations in the getter of course, which can be valuable. You could do the same with a function, but accessing a calculated field like a normal variable is sometimes arguably cleaner for the caller.

You can also do read only properties by only specifying a getter...

 public int MyValue { get { return _myValue; } }

A read only property can be valuable if you want to expose an array property. Users of the object can access the array elements then, but they can't replace the array itself.

You can even do write only by just specifying a setter. There's probably a use case for that but I've never seen one. Something security related maybe.

So... properties are technically the "right" way to do things in an object, but there is some performance cost and can often safely be ignored.

Comment
Add comment · Show 1 · 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 Anxo · Oct 21, 2015 at 06:38 PM 0
Share

All very good answers thank you.

I was really just worried that there were some rules that I was not aware off. Like one version would save the variable under certain circumstances and the other would not, but that does not appear to be the case.

avatar image
1

Answer by tanoshimi · Oct 21, 2015 at 03:52 PM

Unity made a design decision (maligned by some) that whether a variable is serialised and displayed in the inspector is influenced by its access modifier. private variables are, by default, not visible (although can be made to be using [SerializeField]). public variables are visible (although can be made not by using [HideInInspector] and/or [NonSerialized]). Properties are never displayed in the inspector (unless you use a custom inspector component).

Unity itself seemed to change its in-house style used in examples and tutorials from public to [SerializeField] private between 4.x -> 5.x, so you'll see variation in the official examples and documentation. A lot of odd code workarounds that look like they are written to do with access level of certain variables are actually to do with exposing variables to the editor so that they can be changed at edit-time.

Depending on your CS background, you may feel uncomfortable with the idea of making public variables that are not accessed through a property, although in Unity there really is no reason not to. In the code above, the "indirect" property in the second example adds no value whatsoever, unless the template was placed there to allow for additional validation code to be added to the setter at some point in the future.

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
1

Answer by Glurth · Oct 21, 2015 at 03:54 PM

Usually get/set accessors are used when some kind of processing may need to be performed every-time the variableA is read or written. You can put just about any kind of processing you like, inside those get and set functions. Usually this includes stuff like, setting dirty flags, and doing validation, but can also get pretty advanced and do stuff like buffer-large-data-storage and launch co-routines.

Additionally, with inheritance, you can automatically allow children of your class to apply their own processing of variableA, which may be different from that of the parent, and other children. So, when programmers want a base class to have maximum flexibility, they make all the public "variables" out of accessors.

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

31 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Serialize Variables from Items in an Array 1 Answer

How do I make a list of references to a variable? 2 Answers

Is it possible to access a variable in a different script without reference? 2 Answers


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