Search Unity

How to (Or can I) edit variables from inherited class

Discussion in 'Scripting' started by sudoGravel, May 15, 2022.

  1. sudoGravel

    sudoGravel

    Joined:
    May 14, 2022
    Posts:
    5
    I have a class
    Character
    that has the variables
    Code (CSharp):
    1. int health;
    2. float movementSpeed;
    3. float attackSpeed;
    4. //etc...
    And I want to create another class (say Brian) who inherits from the class Character and thus already has those variables assigned to him:

    Code (CSharp):
    1. public class Character_Brian : Character
    2. {
    3.     health = 100;
    4.     movementSpeed = 1.3f;
    5.     attackSpeed = 1.2f;
    6.     //etc
    7. }
    (Of course this doesn't actually work, but it hopefully paints the picture of what I want to do)

    This is so that I can do something like displaying values that can vary on places such as healthbars, like
    Code (CSharp):
    1. public class HealthBar : MonoBehaviour
    2. {
    3.     health = Character.health
    4. }
    so that I don't have to write an if else tower for each character available

    Is this possible to do? And if not, what would be the best way of doing it?
    Thanks in advance
    :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Really smells like you want some of these babies: fresh piping hot ScriptableObjects!

     
    Vryken likes this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You need to make those variables non-private first.
    You can add the public modifier to them, which allows them to be accessed by any other object, or you can add the protected modifier to them, which allows only child objects to access them.

    If you want to initialize those variables from the child class, there's a couple ways to go about it.

    If the parent class is a MonoBehaviour
    You can set the initial values from the
    Awake
    or
    Start
    method:
    Code (CSharp):
    1. public class Character_Brian : Character
    2. {
    3.   void Awake()
    4.   {
    5.     health = 100;
    6.     movementSpeed = 1.3f;
    7.     attackSpeed = 1.2f;
    8.   }
    9. }
    If the parent class is a plain C# object
    You can set the initial values from a constructor:
    Code (CSharp):
    1. public class Character_Brian : Character
    2. {
    3.   public Character_Brian()
    4.   {
    5.     health = 100;
    6.     movementSpeed = 1.3f;
    7.     attackSpeed = 1.2f;
    8.   }
    9. }
    You could even define a constructor that takes in these values as parameters in the parent class to initialize them there, if that's what you prefer:
    Code (CSharp):
    1. public class Character
    2. {
    3.   protected int health;
    4.   protected float movementSpeed;
    5.   protected float attackSpeed;
    6.  
    7.   public Character(int health, float movementSpeed, float attackSpeed)
    8.   {
    9.     this.health = health;
    10.     this.movementSpeed = movementSpeed;
    11.     this.attackSpeed = attackSpeed;
    12.   }
    13. }
    14.  
    15. public class Character_Brian : Character
    16. {
    17.   public Character_Brian() : base(100, 1.3f, 1.2f) {}
    18. }
    But I'd say the ScriptableObject approach mentioned above may be better for this in general.
     
    Last edited: May 16, 2022