Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Longman1981 · Feb 16, 2018 at 08:23 AM · gameobjectprefabsinheritanceeditorgui

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by YasinJavaid_ · Feb 16, 2018 at 11:35 AM

Here i would like to recommend you two architectures

A: Figure A

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.


capture.png (3.2 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Longman1981 · Feb 16, 2018 at 08:56 AM 0
Share

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.

avatar image Harinezumi Longman1981 · Feb 16, 2018 at 12:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges