- Home /
Basic gameobject inheritance
Hi fellows, small one
Is there a way to have objects inherit properties? I know i can attach a script and it will get to interact with it, but think about this example:
Suppose I have several objects which work as "floor blocks" in my game. They are different, but in the end, they all behave as "floor". Now suppose I want my floor object to behave exactly the same yet they are different objects. So I have grass, wooden floor, asphalt, whatever. I wat them all to share its "floor" nature and maybe differ in some other stuff like "player speed" or i donno, damage rate?
How do you guys handle this inheritance scenario? All i can do is to attach the same script? separatelly to them all?
thanks in advance! UNITY IS AWESOME Mauro
Answer by jefjohms · Jun 24, 2014 at 05:02 PM
Sounds like prefabs is what you are looking for. http://docs.unity3d.com/Manual/Prefabs.html
Ok, it's not really what i was looking for (i was talking about a 100% program$$anonymous$$g related question) but still, I guess that prefabs implement some sort of "the unity way" reusability an inheritace...
thanks @jefjohms
Answer by Sisso · Jun 27, 2014 at 01:12 AM
Take a look on this question, I have answer some time ago.
http://answers.unity3d.com/questions/710173/good-inheritance-practice-in-unity.html
It's great, but: How do you actually make your character implement or inherit Damageable
or Controllable
?
You don't.
In a more component oriented architecture you composite your game object with many Components. In your example you can have the Floor component with basic stuff. So you can create complete independent components that you can attach to create a lava like SlideTextureFloor that change the texture over time, and DamageFloor that have a OnCollider event and cause damage to the player.
Your answer
Follow this Question
Related Questions
How do i NOT break encapsulation with Unity? 2 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
Game architecture best practices 3 Answers
OnDrawGizmos for parent and child class 1 Answer
Derived Class Fields 3 Answers