- Home /
Noob question about class inheritance
I'm kinda new to scripting and I've got a basic question about class inheritance in Unity.
Let's say I want to make a base class named Enemy, which will contain the basic features shared by all enemies, and a derived class for every enemy type, for example, Orc and Goblin.
So I've got three different scripts: Enemy (the parent class), Orc and Goblin. And the question is: to which gameObjects should I attach them? I can only imagine the Orc and Goblin scripts must be attached to the Orc and Goblin prefabs, but where do I put the Enemy class?
Thanks in advance!
Answer by LMan · Jul 04, 2016 at 10:06 PM
Simply put, you don't put the Enemy class anywhere- it comes with the inheriting classes automatically. Because Orc and Goblin inherit from Enemy, they get access to all the member variables of Enemy in addition to their own specific variables.
For instance, suppose all your enemies have an integer variable health. You can put health into the Enemy base class, and every class that inherits Enemy will get that variable. On the other hand, if only your Goblins have a float variable magic, you would put magic into the Goblin class. Only the Goblins would have that variable.
Thank you for your really quick answer! For some reason I assumed had to be attached to a gameObject in order to do something, but you're right: if I make my Orc class (attached to the Orc prefab) extend from the Enemy class (which is not attached to anything) it works anyway. Thanks! :)
Answer by Bunny83 · Jul 04, 2016 at 10:10 PM
The answer is: nowhere. The Enemy class is just the base class. Every derived class is also an Enemy. If the Enemy class itself should not be used on it's own you should make it an abstract class. That way it can't be attached to a GameObject.
Thank you for your answer! I'll make some research about abstract classes! Yet, I don't know if it's convenient that they can't be attached to GameObjects in my case:
I have a function that instantiates enemies, and gives them a certain HP value, which is a variable defined in the Enemy Class. I do this with this line of code:
(EnemyInstance.GetComponent("EnemyBehaviour") as EnemyBehaviour).HPValue= 100;
To access this HP value, I have to attach the Enemy Class to the enemies. This way, the Orc has both the Enemy and the Orc class scripts attached, and the Goblin has both the Enemy an the Goblin class scripts attached.
(By the way, all enemies have the same HP, doesn't matter if they're Orcs or Goblins.)
$$anonymous$$aybe there's a more proper way to do this?
No, you still haven't understood what inheritance mean. When you attach an "Orc" script to your object that Orc instance is also an Enemy instance.
So if the gameobject "obj" only has an Orc script attach you can still do:
GameObject obj;
obj.GetComponent<Enemy>().HPValue = 100;
Since the "Orc" script is derived from "Enemy" you can treat an Orc as Enemy. So the GetComponent call will return the Orc instance as Enemy.
Attaching an Orc script as well as an Enemy script to the same object doesn't make any sense since everything inside the Enemy class will be present twice. Also GetComponent<Enemy>()
could return either of the two instances since both are Enemy instances.
You're right, it works! Thank you for your answer, sir!
Your answer
![](https://koobas.hobune.stream/wayback/20220612084229im_/https://answers.unity.com/themes/thub/images/avi.jpg)