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 /
This question was closed Feb 06, 2014 at 05:16 AM by Eric5h5 for the following reason:

Too subjective and argumentative

avatar image
2
Question by Wisteso · Feb 06, 2014 at 03:12 AM · inspectorpropertiesassign-variable

Why does Unity3D encourage use of public variables instead of setters/getters?

While there are other examples, the most notable case is that only public variables are exposed to the inspector window.

By encouraging use of public variables, this also encourages new programmers to avoid the use of properties and setters/getters, which are established to be much better practice than using public variables for the same task.

You can write a custom inspector to get the proper behavior, but I am wondering if I am missing something with understanding why Unity didn't provide this out of the box, yet they provided inspectors which encourage the bad practice.

Background:

  • http://c2.com/cgi/wiki?GlobalVariablesAreBad

  • http://stackoverflow.com/questions/484635/are-global-variables-bad

  • http://en.wikipedia.org/wiki/Global_variable (first section)

Comment
Add comment · Show 6
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 Lairinus · Feb 06, 2014 at 03:56 AM 0
Share

It's my understanding that public variables in Unity are strictly meant for debugging / prototyping. It's a lot more convenient to select your main character and change the "speed" field than it is to open up your IDE and find the exact spot for the code and change the value that way. Granted it doesn't take that much longer, but if you have to test out 100 different values, public serialization saves a few $$anonymous$$utes.

Personally, whenever I do prototyping and debugging, I use [SerializeField] private variables just so nothing extra would plug up my intellisense and so that I can keep some of my fields semi-immutable. Even for a lot of non-debugging issues, I use [SerializeField] where convenient.

Lastly, it's a matter of personal preference, and it probably isn't intended to deter new programmers from properties. The current state of serialization via Public variables is fine and great. However if new programmers spam public fields then they'll learn the hard way that it makes coding 10x harder. At that, if a new programmer jumps into Unity with absolutely 0 coding knowledge expecting to make something great, public fields will be the least of their worries.

avatar image RyanZimmerman87 · Feb 06, 2014 at 04:16 AM 0
Share

I just read the majority of :

http://c2.com/cgi/wiki?GlobalVariablesAreBad

Now I'm a self taught programmer with only a little over a year of experience, so take this with a grain of salt haha.

But I really think some people severely over think certain details like this (I've seen many similar posts with people really putting a lot of effort to complain about C# or other program$$anonymous$$g aspects in Unity).

I think Unity is just not designed in a way that will seem "scientifically correct" to a lot of heavily schooled computer science majors. Or in other words some of what you learned while designing other programs and studying computer science in general is not necessary with Unity and may even be counter productive.

Unity is designed in a way that it's really easy to work with. It's supposed to do the hard work for you and let you be creative with your game design.

I read a lot of that link and to me it just sounded like "Beware of public variables or public static variables they are bad if you don't use them correctly!" Well of course they are bad if used incorrectly! But both public variables and public static variables are incredibly useful and easy to work with in Unity and I don't really see any downsides to doing so in my experience.

avatar image Wisteso · Feb 06, 2014 at 04:41 AM 0
Share

@RyanZimmerman87 The issue isn't just that using public variables is dangerous. It's also pretty limiting. Per my response to the below answer, you can't do things like input validation/processing or locking.

avatar image RyanZimmerman87 · Feb 06, 2014 at 04:48 AM 0
Share

You are probably right there are a LOT more potential problems than what I'm thinking about since I'm kinda a noobie programmer still.

But I would have to imagine you could do input validation/processing or locking just by adding unique logic to your scripts to make sure things are correct or working as expected?

For example when you get new input that overwrites the old one you could temporarily store the old one and then after processing replace the new one if it's not correct? $$anonymous$$aybe I'm just not understanding a lot of these more complex program$$anonymous$$g terms or what you can't do with public variables.

avatar image _Keagan · Dec 28, 2016 at 07:08 PM 1
Share

@Wisteso I want to ask as an aside, what it the point of using a public property where both get and set are are not given any extra logic controlling the reading and writing of a var? This is $$anonymous$$Y PERSONAL OPINION haha, but I think only use a property when you need to protect how it is read or written to. Unity actually has a tutorial on properties and does a good job of explaining a useful use case.

Show more comments

1 Reply

  • Sort: 
avatar image
3

Answer by Ashish Dwivedi · Feb 06, 2014 at 03:55 AM

It is not compulsory to use public to expose variable in inspector. There is an attribute [SerializeField] which is used to expose private variables to inspector.

SerializeField Attribute Reference

Comment
Add comment · Show 2 · 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 Wisteso · Feb 06, 2014 at 04:26 AM 0
Share

This definitely helps to keep the API clean, but the other primary issue is that there's no ability to perform input validation, pre/post-processing, or apply locking.

avatar image iwaldrop · Feb 06, 2014 at 07:40 AM 0
Share

You can implement getters and setters using the private variables as backing data if you wish, where design-time validation would done via custom inspectors. You can even wire the inspector to actually get/set the properties for DRY code.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Hide/Show properties dynamically in inspector. 6 Answers

Assign video in inspector via script 0 Answers

Preventing users from editing specific properties in any arbitrary inspector _without_ creating custom inspectors? 0 Answers

If I have a bool method, how can I see in the inspector whether it returns true or false? 2 Answers

How to get the target object value from a PropertyDrawer for an array of objects? 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