Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question public variable vs private and Getter

Discussion in 'Scripting' started by gamer123454321, Apr 27, 2021.

  1. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    Hi! What is the main reason why using private variable then use Getter instead of just public the variable?
    For example:

    public GameObject unit;


    VS


    private GameObject unit;

    public GameObject GetUnit(){
    return unit;
    }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    While newer to coding myself, as far as I understand the reasoning is it sets security when accessing variables from outside the script. If you have a variable that you only want to be read from outside, you can use just the get accessor.

    Though the example you've shown is a method rather than a property, and generally not how they would be used. If you're specifically just wanting to return a value that's available within a script, you'd do the following:

    Code (CSharp):
    1. public Gameobject Unit
    2. {
    3.     get
    4.     {
    5.         return unit;
    6.     {
    7. }
    If you want to be able to set the value as well, you'd use a 'set' accessor. The beauty of properties as well is that they don't just need to return a variable already set, but can perform any logic that you want. A simple example is using a property to check if an entity is grounded, performing a raycast inside the property.

    Generally you'd use methods like in your example if you need to pass in a variable.
     
    PutridEx likes this.
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    It's called Encapsulation and the gist of it is to ensure the state of the object remains valid and consistent.
     
    DonPuno likes this.
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455

    If your variable is public than you cant see who access it.So if you will have some bug in your code what will set some wrong value to your variable then is much easier to find id with getter or setter.
     
    DonPuno likes this.
  5. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    272
    Dinifition of encapsulation: In object-oriented programming (OOP), encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components.

    If something should be used and changed only inside class then it should be private. Some properties should be changed only inside one class, but be read-only in others.
    For example you made PlayerMovement class, and then you have MovementAnimator class which do something like:
    Code (CSharp):
    1. animator.SetBool("IsGrounded", movement.IsGrounded)
    In that case, you can write code like this inside your Movement class:
    Code (CSharp):
    1. private bool _isGrounded; //for operations inside class
    2. public bool IsGrounded => _isGrounded;
    or like this:
    Code (CSharp):
    1. public bool IsGrounded { get; private set; }
    The reason of all of that is to save yourself and especially other programmers (if you work in team) from mistakes.
     
    DonPuno likes this.
  6. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    83
    thank you everyone:)