0

For context, I got this code from this tutorial, particularly at 2:27:28.

I have two classes, Player and Warrior. I want the Warrior class to inherit from the Player class. The Player class looks like this:

public class Player: MonoBehaviour
{
    //Variables and Properties
    private string _playerName;
    public string pName { get => _playerName; set => _playerName = value; }

    private int _playerHealth;
    public int Health { get => _playerHealth ; set => _playerHealth = value ; }

    private int _playerPower;
    public int Power { get => _playerPower; set => _playerPower = value ; }


    //Constructors
    public Player() { } //Constructor 1

    public Player(string name, int health, int power) //Constructor 2
    {
        pName = name;
        Health = health;
        Power = power;
    }
}

And the Warrior class looks like this:

public class Warrior : Player
{
    public Warrior(string name, int health, int power)
    {
        pName = name;
        Health = health;
        Power = power;
    }
}

Whenever I remove Constructor 1 in Player (the blank one), I get an error in the Warrior constructor. But if I remove the Warrior Constructor itself, then I get an error when declaring the Warrior class itself. I don't understand why it would be necessary to add Constructor 1, especially when I already have Constructor 2 in the Player class, to begin with.

Is there something I am missing? Or does a parent class always need an extra constructor whenever the child is being declared? And if so, Why? Or maybe inheriting constructors in UNITY works differently?

I tried removing Constructor 1 and using the command CTRL + . in VS, and even the debugger confuses me. I also tried googling online to no avail.

Shane Amare
  • 25
  • 1
  • 10
  • 1
    `Player` defines `pName`, `Health`, and `Power`, so `Warrior`'s constructor should pass the parameters that are assigned to them to `Player`'s constructor (you've labeled it "Constructor 2"). The derived class shouldn't take responsibility for initializing the base class. – madreflection Jun 29 '23 at 01:09
  • 1
    `Player`'s "Constructor 1" (the *default constructor*) shouldn't exist because `Player` shouldn't ever be instantiated without being given values to initialize its properties. – madreflection Jun 29 '23 at 01:10
  • 1
    See https://stackoverflow.com/questions/23481456/passing-parameters-to-the-base-class-constructor – madreflection Jun 29 '23 at 01:10
  • 5
    I haven't looked at the tutorial you're referencing but you're not supposed to use constructors *at all* in `MonoBehaviour`. –  Jun 29 '23 at 01:10
  • Ugh... `MonoBehaviour`... that thing needs to come with a warning label. – madreflection Jun 29 '23 at 01:14
  • 2
    in addition to @madreflection first comment: You should/would usually also no go through the properties in the constructor but rather set the fields ... except in your case you could even use auto-properties – derHugo Jun 29 '23 at 09:12
  • @rshepp Oh... so I just can't, or rather shouldn't, use constructors in unity? But rather the Awake(), OnEnable(), and Start() methods by manually assigning values to properties in them? – Shane Amare Jun 29 '23 at 22:16
  • 2
    You can use constructors in the classes you create if they don't derive from a Unity object, e.g., `MonoBehaviour`, `ScriptableObject`, etc. The engine handles construction of these objects. Instead of writing `Player player = new Player();`, you would write `GameObject player = Instantiate(playerPrefab);`. If the object's initialization depends on whatever spawned it, then you could write a public `Init(value1, value2, etc)` method to allow the spawner to provide some initialization values after instantiation. If the object can initialize itself, you can use Unity events like Start, etc. –  Jun 30 '23 at 02:40
  • @rshepp Understood. Thank you! – Shane Amare Jul 02 '23 at 00:21
  • @rshepp One last question. Do you think I should reformat/reword my question to ask more so about Constructors in Unity? And if so, how do you think I should go about it? – Shane Amare Jul 03 '23 at 14:11

1 Answers1

-1

Like rshepp said, you probably shouldn't use constructors with MonoBehaviors. Instead, use Start() and Awake() to initialize.

Jflyer45
  • 47
  • 3
  • 1
    While I agree with you, it would have also been useful to answer the question since not all classes in Unity are MonoBehaviours. – Ash Jul 27 '23 at 16:15