- Home /
How to create inheritance for unity GameObjects
Basically what I'm trying to achieve is something similar to OOP inheritance but with using unity's GameObjects. for example I have Dog gameobject It has many components attached:
Health script
Experience script
Running script
Rigidbody2D
Collider
you get the point...
Now I need to add also cat and shark. So basically what we'd need is to create inheritance with gameObjects e.g:
Animal -> fish -> shark,
Animal -> Mammal -> dog,
Animal -> Mammal -> cat.
What's best way to achieve that? Only way I see is to create Animal prefab, than take this prefab modify it,(e.g add swimming) break prefab reference, than create new prefab from it called - Fish. Than do same for Mammal. Than again do same for Dog and Cat but that doesn't seem good. You don't see how inheritance is built and connection to parent is lost.
Answer by YasinJavaid_ · Feb 16, 2018 at 11:35 AM
Here i would like to recommend you two architectures
A: Figure
use this model. Create base class put all basic methods in it and other on their classes.
Class Animal{
public walk();
public eat();
public xyz();
}
class Mamal:Animal{
public walk(){}
public eat(){}
public xyz(){}
}
class Cat:Mamal{
void Mew(){}
void Etc(){}
}
class Fish:Animal{
public walk(){//swim here}
public eat(){}
public xyz(){}
}
class Shark:Fish{
void jump(){}
void Attack(){}
}
attach all classes to their objects.
B:
make one object attach all scripts and components. load functionality at run time.
Answer by Harinezumi · Feb 16, 2018 at 08:51 AM
This is just my opinion, but I wouldn't model a class hierarchy after biological classification. Does knowing that something is a mammal or fish help in the functionality? Fish swim, but so do dogs, and when needed, cats as well.
I would probably just have one Animal class, and use various components to model the behaviour of the different animals.
As for creating new prefabs from an existing one, the best way I know is to drag the prefab to the hierarchy, rename it, then drag it to a folder in the project view. That is, breaking the prefab instance is not necessary if you rename. I tested this in Unity 2017.3, but it should work in earlier versions as well.
Cats/Dogs staff was just an example. But $$anonymous$$ain issue I'm facing is that I want to create inheritance with GameObjects ins$$anonymous$$d of some particular class. I want to have ready gameobject with all common components attached, than use it as base gameobject and so on.
For me the difference between animals is their model and the possible behaviours (which are modeled by components), but I will stop questioning the why, and trust that you know what you want.
In which case I think your approach of creating Animal prefab, then base on that a $$anonymous$$ammal prefab, and on that the specific animal prefab makes a lot of sense. In case you need to know the specific type in code, you can create a class hierarchy as suggested by @Yasin063, but making Animal, $$anonymous$$amal, Fish, and Bird classes abstract, and overriding only in the specific classes like Shark and Dog.
Your answer
Follow this Question
Related Questions
Adding script during runtime 2 Answers
Create with code 2.2 pizza not shooting 1 Answer
Mark gameobject field as changed from prefab 2 Answers
Prefab variants inherited scripts 1 Answer
Automatically update prefab instance 1 Answer