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

Reason for declaring a variable using protected access modifier instead of using auto property?

Discussion in 'Scripting' started by egonspengler_84, Feb 23, 2021.

  1. egonspengler_84

    egonspengler_84

    Joined:
    Jan 26, 2021
    Posts:
    153
    Hi there,

    I'm learning about state machines in Unity by following a Youtube tutorial. The finite state machine that is taught in this video uses four C# scripts:

    1. Player
    2. PlayerStateMachine
    3. PlayerState
    4. PlayerData

    In the PlayerState class(which is supposed to be a base class) he creates a handler to the other scripts by writing the following:


    Code (CSharp):
    1. public PlayerState{
    2.  
    3. protected Player player;
    4. protected PlayerStateMachine stateMachine;
    5. protected PlayerData playerData;
    6.  
    7. }

    He says the reason for using the "protected" access modifier when creating these three handlers is because classes that inherit from the PlayerState base class will also be able to access these variables/handlers. Which makes me wonder if it's possible to use an auto-property to declare these variables instead of using "protected" like so:

    Code (CSharp):
    1. public PlayerState{
    2.  
    3. public Player player{get; private set;}
    4. public PlayerStateMachine stateMachine{get; private set;}
    5. public PlayerData playerData{get; private set;}
    6.  
    7. }

    Would I be totally wrong to create these variables using auto-properties as shown above? Is it better to declare them with "protected" access modifiers?
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    variables (and any other field) can have 3 "types", private, protected and public

    private - only accessible from this class

    protected - accessible from this class and any class inheriting

    public - anyone can access this field

    if you declare
    Code (CSharp):
    1. public PlayerState{
    2. public Player player{get; private set;}
    3. public PlayerStateMachine stateMachine{get; private set;}
    4. public PlayerData playerData{get; private set;}
    5. }
    6.  
    you can read(get) from anywhere but write(set) only from this class

    not sure why you're calling them auto-properties, and they aren't "better" or worse - they do different stuff.
     
    Bunny83 and Joe-Censored like this.
  3. egonspengler_84

    egonspengler_84

    Joined:
    Jan 26, 2021
    Posts:
    153

    I call them auto-properties as it's just that when I was following a Youtube tutorial on properties the tutor said that auto-properties are the same as normal properties with a set and get accessor except it consists of one line of code. And they are used if you don't intend on doing calculations within the get and set accessors. Here's a timestamp of the tutorial where he talks about auto-properties:


    So instead of writing:


    Code (CSharp):
    1.  
    2. public int _myAge = 12;
    3.  
    4. public int Myage{
    5.   get{
    6.  
    7.        return _myAge;
    8.    }
    9.  
    10.    set{
    11.  
    12.        _myAge = value;
    13.    }
    14.  
    15.  
    16. }
    You write this which is what the guy called an "auto-property" according to him:

    Code (CSharp):
    1. public int MyAge{get; set;}
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,266
    I think what SparrowsNest is trying to say is that whether a value is an auto property or not has nothing to do with whether it's public or protected. A variable can be public or protected. An auto property can be public or protected.
     
  5. BulbasaurLvl5

    BulbasaurLvl5

    Joined:
    May 10, 2020
    Posts:
    42
    As sparrwosnest stated, different declarations are used for different things.

    Your auto-properties can be read by any other object and set only by itself. A protected variable or property can only be accessed by childs of this class. Thus, protected is used if you plan to use inheritance and only need the child to use this variable. If you need other objects to also access it, than you go with your property declaration. If you need other objects to also edit these you go with public
     
    egonspengler_84 and Lekret like this.
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,904
    If you want to simplify, only use public. At first, never use private, protected or auto-properties. They don't actually do anything anyway. Auto-properties do absolutely nothing. Private/Protected do nothing in the sense of how locking your car does nothing -- it has no effect on how the car works. If you stay overnight at a friend's house in the country, leaving your lights on is bad, but forgetting to lock it is nothing.
     
    Joe-Censored likes this.
  7. egonspengler_84

    egonspengler_84

    Joined:
    Jan 26, 2021
    Posts:
    153

    When you say "A protected variable or property can only be accessed by childs of this class." Do you mean a protected variable can only be read when I make an instance of a class?
    So if I had a protected int variable called "TestResult" that had a value of 75 that was stored in a class called "Tests" and I wanted to get access to the "TestResult" variable in a different class called "Student" I would have to make an instance of the "Tests" class within the Student class? Is this correct?

    Could you kindly provide an example of the usage of inheritance with a protected variable? I'm not asking out of laziness, I just don't fully understand what you mean by "protected is used if you plan to use inheritance etc".
     
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,904
    The protected keyword is standard in lots of languages "programming protected example" gives examples. But it's about sub-classes inheriting from a base class so may be a bit complicated.
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,832
    No. Nothing to do with instances. public/protected/private can all be applied to static members and they work just the same as on non-static members.

    "protected" is about inheritance.

    The original class can access everything.

    Subclasses--ones that inherit from the original class, directly or indirectly--can access public and protected stuff, but not private.

    All other code can only access public stuff (not protected or private).
     
    Joe-Censored and BulbasaurLvl5 like this.