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 ProtoTerminator · Feb 13, 2014 at 01:30 AM · c#settersgetters

Get and Set difference

What's the difference between doing this

 public byte getCurrentMoves(){
     return currentMoves;
 }
 public void setCurrentMoves(byte info){
     currentMoves = info;
 }



And doing this?

 public byte CurrentMoves {
     get { return currentMoves; }
     set { currentMoves = value; }
 }

Besides length of code, why is or isn't the second way better?

Comment
Add comment · Show 1
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 ProtoTerminator · Feb 13, 2014 at 04:14 AM 0
Share

Is either way more efficient?

2 Replies

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

Answer by whydoidoit · Feb 13, 2014 at 04:18 AM

The second method lets you make a property that looks like a variable:

   CurrentMoves = 1; //Calls the set method
   print(CurrentMoves); //Calls the get method

Obviously the first way is more wordy and doesn't look like a normal field:

    setCurrentMoves(1);
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 whydoidoit · Feb 13, 2014 at 04:18 AM 0
Share

There's no notable performance issue either way.

avatar image blackkeath88 · Dec 13, 2019 at 02:14 AM -1
Share

/ can you also help me with my script ***receiving error "; expected" and get/set does not exist in the current context

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraFollow : $$anonymous$$onoBehaviour { private Transform player; private float damping = 2f; private float height = 7f; private Vector3 startPos; private bool canFollow;

 // Start is called before the first frame update
 void Start()
 {

     player = GameObject.FindWithTag("Player").transform;
     startPos = transform.position;
     canFollow = true;
     
 }

 // Update is called once per frame
 void Update()
 {
     Follow();
     
 }

 void Follow()
 {
     if (canFollow)
     {
         transform.position = Vector3.Lerp(transform.position, new Vector3(player.position.x + 10f, player.position.y + height, player.position.z - 10f), Time.deltaTime * damping);
     }
 }


public bool CanFollow() { get { return canFollow; } set { canFollow = value; }

 }



} // class

avatar image Bunny83 blackkeath88 · Dec 13, 2019 at 03:23 AM 0
Share

Don't post questions in comments to other questions. If you have a concrete and specific question, ask a seperate question. Anyways, remove the method brackets from your property declaration. It's a property, not a method.

So it should be

 public bool CanFollow { get ...........

ins$$anonymous$$d of

 public bool CanFollow() { get ...........
avatar image
1

Answer by poncho · Feb 13, 2014 at 05:00 AM

The difference between methods and properties, is just to the programmers, methods mean actions, while properties mean... well, properties, a description or feature of something. to your immediate example would be like getCurrentMoves() would mean, getting the info of the variable currentMoves, and setCurrentMoves would mean, setting the bytes to currentMoves variable, which does not feels quite right, on the other hand, if you use it as a property (which could be just public byte CurrentMoves{get;set;}), it implies that you will get and set values to the CurrentMoves property. I think i could not explain myself well enough, but hope you get something from my explanation

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

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Getters/Setters with C# Array 1 Answer

Getter / Setter and Variable Scope Question? 1 Answer

Semi Generic With References 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