• 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 EndlessArray · Aug 02, 2013 at 03:27 AM · c#property fields

Public properties vs variables?

Qucik question: In all the tutorials I see, all the scripts are made with public variables and no one uses properties. Is there a particular reason for that?

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

2 Replies

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

Answer by Jamora · Aug 02, 2013 at 03:58 AM

I think there is.

Most new Unity users want to make things happen, they don't necessary want to know why they happen or how to do it effectively. Because of this. they probably won't care about encapsulating data, maintainable code or any other good programming practice either. I'd imagine most tutorial writers are either in the mindset "I'll just show them, in as few lines of code as possible, how to make this happen." or don't know any better themselves.

So, to answer your question, using public variables is just lazy programming, for the points mentioned above. < Rhetoric question > But then again, why write 4 lines of code when you could just write 1 for the same functionality? < /Rhetoric question >

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 Andrew-Le · Jan 03, 2017 at 03:35 AM 0
Share

Sorry for digging this up xD but i really want to know when you use properties ins$$anonymous$$d of variables(lazy things) !!

please i have seen many topic about c# which they answered different between them !!

but i really need some realistic case !! please answer me if u got this ^^ ty sry for my bad english !!

avatar image
2

Answer by EndlessArray · Aug 02, 2013 at 03:54 PM

So, I just tested a random hunch, and it turns out it has more to do with how the unity editor works and less with lazy programming. Public variables are visible in the editor and it's values can be set from there. public properties are not shown in the editor.

Comment
Add comment · Show 3 · 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 robhuhn · Aug 02, 2013 at 04:09 PM 2
Share

In that case you would serialize the field to make it visible in the inspector:

 [SerializeField]
 private int myInt = 3;
     
 public int $$anonymous$$yInt {
     get {
         return this.myInt;
     }
     set {
         myInt = value;
     }
 }
avatar image MaDDoX robhuhn · Oct 11, 2015 at 05:43 PM 0
Share

But that would completely defeat the purpose of properties, firing up "onChanged", "onRead" events and encapsulating the data.. You can always do a custom editor for properties, but ppl usually don't bother unless they need buttons and stuff.

avatar image Statement MaDDoX · Oct 11, 2015 at 05:53 PM 0
Share

Well, you could write your own property drawer that monitor the field and respond to inspector changes. Though I find it rare to respond to changed values at edit-time.

But in general I don't see having public fields for reference binding a big problem. I try to design my code to not rely so much on them. In fact, I would be thrilled to see a serious discussion in the forums showing cases where having public fields being a concrete problem, in Unity, specifically.

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.

Welcome to Unity Answers

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.



Follow this Question

Answers Answers and Comments

19 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity editor not saving object changes 4 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges