• 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 Jan 13, 2016 at 08:34 PM by tanoshimi for the following reason:

Too subjective and argumentative

avatar image
0
Question by Play Creatively · Jan 13, 2016 at 06:56 PM · variablefunctiongetter

Should I make variables instead of functions?

I recently discovered the power of getters/setters and I found out that instead of making functions that return somet$$anonymous$$ng you can rather make variables that return somet$$anonymous$$ng.

Are there any pros and cons for using either? I just can't decide on w$$anonymous$$ch to choose and I want to know more about it before I get a habit of doing somet$$anonymous$$ng I'll have to change in the future.

I know that variables can't pass parameters so I'll obviously just use a function in that scenario.

I wanna hear your thoughts.

Comment
Add comment · Show 3
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 Munchy2007 · Jan 13, 2016 at 08:31 PM 0
Share

There are pros and cons for each approach. Your post however, isn't a question and should really have been made in the discussion forums.

avatar image Play Creatively Munchy2007 · Jan 13, 2016 at 11:07 PM 0
Share

There is a discussion forums! Can you link me to it?

avatar image Munchy2007 Play Creatively · Jan 13, 2016 at 11:17 PM 0
Share

http://forum.unity3d.com/

2 Replies

  • Sort: 
avatar image
1

Answer by tanoshimi · Jan 13, 2016 at 08:32 PM

A function does somet$$anonymous$$ng. A variable is somet$$anonymous$$ng. They're not interchangeable and you're going to need both in your code...

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 thesleeve · Jan 13, 2016 at 10:33 PM

I believe the proper term for t$$anonymous$$s is Properties, in C# lingo. A Property is accessed like a method (function) but it acts like a field (variable). Properties usually have getters and setters, as you mentioned.

Properties can be really useful because it allows an object to manage its own data. Here's a simple example from MSDN:

 public class Date
 {
     private int month = 7;  // Backing store
 
     public int Month
     {
         get
         {
             return month;
         }
         set
         {
             if ((value > 0) && (value < 13))
             {
                 month = value;
             }
         }
     }
 }

T$$anonymous$$s allows the object to make sure that it doesn't accept invalid data. Months only make sense if they're between 1 (January) and 12 (December), and t$$anonymous$$s class will not allow its month to be set to any values that don't conform to that restriction.

Basically, t$$anonymous$$s will help you from introducing bugs by setting values that don't make any sense.

Comment
Add comment · Show 4 · 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 Play Creatively · Jan 13, 2016 at 11:00 PM 0
Share

"Properties", okay. Gonna add that in my notepad, thanks! Still, as properties are basically functions, are there any pros/cons for using one over the other? The only recognizable difference is that you'll get to skip the () part for the properties which looks cleaner if anything.

avatar image thesleeve Play Creatively · Jan 14, 2016 at 12:00 AM 0
Share

Not sure if you read the rest of my post...

With a field (a.k.a. variable) you can do this: Date.month = 13; ...and it will actually set the month to 13. The problem, of course, is that there's no such thing as the 13th month in the year. It doesn't exist. There's no protective mechanism in place to prevent you from setting it to an invalid value.

Whereas, if you are implementing a property, as I described in my earlier post, you write this: Date.Month = 13; And the month will not change. That's because in the accessor (the set function), you provide this condition: if ((value > 0) && (value < 13)) { month = value; } This prevents the month from being set to an invalid value.

avatar image Play Creatively thesleeve · Jan 14, 2016 at 09:32 PM 0
Share

Oh, sorry, I understand. Wasn't really thinking about the fact that you can change the value of the variable (off course duh). The example I had stuck in my head was like: direction { get { return (vectorA - vectorB) } } That I can do either way, in this form (property) or a function that does the same. So when I found that out, I just though why would I ever do this in a function format. I guess in this case it's okay to use a property despite this being perfect for a function: ( direction() ) I just have been using functions for so long that I thought there would be a catch for using a property instead of a function in this scenario.

Show more comments

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

script with individual function for each gameObject or public script variable to solve inventory system problem 1 Answer

iTween - calling functions on oncomplete doesn't work if the function is declared as a variable 1 Answer

How to correctly send a variable to another function? 1 Answer

Calling non applied scripts 3 Answers

Cleaning up my item script... Passing script name to function? 0 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