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 May 19, 2015 at 05:17 AM by Fattie for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by Gilead7 · May 18, 2015 at 11:18 PM · c#getset

When to use SETters and GETters

When and where should you use setters and getters? I'm guessing it's instead of public variables for a structure?

Comment
Add comment · Show 2
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 Eno-Khaon · May 18, 2015 at 11:31 PM 0
Share

In part, they're mainly there to create context for the data you're obtaining.

For example, say you have an enemy with 54,037,928 health. Rather than having that variable be public, you set it as private and use GetHealth() to find its current health. Then, you don't have to report the health exactly.

In this manner, you can return a value as precise or imprecise as you want. $$anonymous$$aybe ins$$anonymous$$d of saying it has 26,044,283 health remaining, you would prefer to say that it has 48% (or 48.1%) health remaining. $$anonymous$$aybe you'd rather just round it to the nearest million? That's entirely up to you.

avatar image Fattie · May 19, 2015 at 05:17 AM 1
Share

this is a general program$$anonymous$$g question, and simply adds to the clutter here. go ask on stack overflow.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by nug700 · May 18, 2015 at 11:33 PM

You use them to run code when the user read/writes to the variable.

In other words you use them WHEN YOU NEED TO DO SOMETHING when they are changed or perhaps read.

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 fafase · May 19, 2015 at 06:08 AM 0
Share

$$anonymous$$ainly they make code easier to read. Java does not have get/set and it works fine.

in C++ there were time when youwould end up with:

   member->SetValue(member->value++);

now with C# get/set;

    member.value++;

avatar image
2

Answer by GabLeRoux · May 19, 2015 at 10:03 AM

setters and getters are mutators and accessors, they are methods used to control changes to a variable.

You can control access level for both the getter and setter methods independently (ex: have the getter public, but have the setter private so a different script can read it, but won't be able to change it). You can even do some additional logic every time any script tries to access it.

example:

 using UnityEngine;
 using System.Object;

 public class SomeClass : MonoBehaviour
 {
     // you could add [SerializeField] here
     private Object obj;
     private Object counter = 0;

     // getter
     public Object getObject() {
         counter++; // side effect (unless really required, may be considered as bad practice)
         Debug.Log("Someone asked " + obj.ToString() + " " + counter + " times!"); // will output number of Object read to the console
         return obj;
     }

     // setter
     private setObject(Object objParameter) {
         obj = objParameter;
         // some other logic
         // if (something) { doSomething(); }
     }
 }

Note that getObject is public and setObject is private.
The obj variable won't show in the Unity inspector as it is private.
You could add SerializeField on top of it to display it in inspector.
Watch out for side effects.
Sometimes, it can be very handy to use Mathf.Clamp in your setter to make sure value is always between two values.

You can refer to differences-between-getter-and-setter-and-a-regular-variablec on stackoverflow for other differences and here's a similar Unity related question. You may also read about Properties on msdn documentation

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

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

Are properties variables? 1 Answer

C# public parameter with custom get/set doesnt shown in the Inspector 4 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Are there differences between initializing at start and at declaration part 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