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
0
Question by MrZalib · Jan 24, 2014 at 04:27 PM · c#designproperties

Advantages of properties?

I feel myself being confused by Properties. Say for instance I am making a Galaga remake or something and I have a class ShipClass which a baseSpeed variable that I want all of the enemies and player to inherit from. From there they can modify that variable which means I can control how fast different enemies go or I can give the player a power up which modifies speed for a little bit. I did this through properties with my ShipClass looking something like this.

using UnityEngine; using System.Collections;

public class ShipClass : MonoBehaviour { //private int health = 100; public int speed = 10;

 public int propSpeed
 {
     get
     {
         return speed;
     }
     set
     {
         speed = value;
     }
 }


}

And then my Player control would look something like this:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour 
 {    
     ShipClass player = new ShipClass ();
 
     void Start () 
     {
         //useless for now
     }
     
 
     void Update () 
     {
 
         if(Input.GetKey("a"))
             transform.Translate (-1 * player.propSpeed * Time.deltaTime, 0, 0);
         if (Input.GetKey ("d"))
             transform.Translate (1 * player.propSpeed * Time.deltaTime, 0, 0);
     }
 }

So my question really is there any advantage in using a property rather than just making a getter and setter method as needed? Or is this purely a programer's preference in which way you implement this? Like choosing between properties, methods, or just making the variable public and let the gods decide the fate of your program.

I hope this is clear enough to understand. Thanks in advance

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

Answer by oatsbarley · Jan 24, 2014 at 04:32 PM

See: What is the difference between a field and a property in C#? - Stack Overflow

Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.

Kent points out that Properties are not required to encapsulate fields, they could do a calculation on other fields, or serve other purposes.

GSS points out that you can also do other logic, such as validation, when a property is accessed, another useful feature.

The two biggest benefits, as mentioned in the quote, are being able to change the field without it affecting users of the class, and being able to perform calculations on get and set. It's handy being able to modify the internals of a class without having to then go to every place the class is referenced and make modifications.

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 MrZalib · Jan 24, 2014 at 04:43 PM 0
Share

Thank you this clears up quite a bit.

avatar image
1

Answer by Owen-Reynolds · Jan 24, 2014 at 07:08 PM

There is no advantage is having a regular variable, such as speed, start life as a property (which is the same thing as saying don't bother writing get/set methods if they don't do anything.)

The advantage to properties is you can easily later convert if you need to. Which, as noted above, is like magically pasting code in front of every speed=X everywhere.

In your example, don't use propSpeed. Just have everyone use player.speed=... to access the variable. Next, imagine you want to enforce a max speed, and maybe also make a rocket emitter change based on speed. Now you change speed into a property: make a new private class variable named _speed, and a getter/setter named speed. Have the setter check the max and change the emitter.

Everywhere in the program where you were using the old variable, speed=, is now a magically doing that new stuff, as a property.

In the old days, w/o properties, you'd have to write a method named setSpeed(float newSpeed);, which would have the same code (check the max, change the emitter.) But then you'd need to go through every other file, changing speed=X to setSpeed(X);. It wouldn't be that bad, since you'd set speed to private and follow the errors (which would force you write also write getSpeed().)

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 MrZalib · Jan 26, 2014 at 01:34 PM 0
Share

Thank you Owen this also clears up quite a bit.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

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

Looking for advice about changing levels. 1 Answer

C# - Doesn't Unity support Expression Bodied Properties? 3 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