Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by V1ncam · May 18, 2014 at 07:28 PM · inheritancepractices

Good inheritance practice in Unity?

Hi

I've been using a style like this:

 abstract class Character : MonoBehaviour { }
 
 class Enemy : Character { }
 class Player : Character { }

Now I saw in an Unity example that they do not inherit the MonoBehaviour part, but separate it. Like this:

 abstract class Character { }
 class Enemy : Character { }
 class Player : Character { }
 
 class EnemyBehaviour : MonoBehaviour { }
 class PlayerBehaviour : MonoBehaviour { }

In hindsight the second case seems more fitting. But I don't know how to correctly implement a class into the behaviour while keeping it scale- and maintainable.

What is the correct practice when using the second case?

V1ncam

Comment
Add comment · Show 4
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 Owen-Reynolds · May 18, 2014 at 09:19 PM 1
Share

Wait ... "Now I saw in an Unity example..."? Like, it was in a blog post on geocities, right after 10 reasons zombies are better than pirates?

avatar image V1ncam · May 20, 2014 at 01:11 PM 0
Share

What? It was an example from the Unity website. If that is what you mean.

avatar image Mizipzor · May 20, 2014 at 01:14 PM 0
Share

Could you add a link to the example you saw in your question?

avatar image Owen-Reynolds · May 20, 2014 at 07:22 PM 0
Share

That's what I mean -- it was just some website. $$anonymous$$ost of the "random" stuff I see about Unity is either deliberate lies, or crack-pot opinions.

Gamasutra is running more Unity3d articles. Look in the comment sections for examples of internet wrongness. People say Unity can't do X, when a quick google leads to UnityDocs or UA and shows otherwise.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Sisso · May 18, 2014 at 07:41 PM

There is never a real "correct way" of do things, what exist is trade offs. Its all depending of context. In your example, I prefer the simple one, but I could use the second one if a is multiplayer game because I can create instance without a GameObject and are easy to serialize.

If you don't see any benefit in the change, don't change, or try to experiments both implementations to see.

You can have something like it:

 class Character : MonoBehaviour { }
 class EnemyBehaviour : MonoBehaviour { }
 class PlayerBehaviour : MonoBehaviour { }

Personally I like a more component by functionality:

 // playe or enemy
 class HasTeam  : MonoBehaviour { }
 // can be target or destroyed
 class Damageable  : MonoBehaviour { }
 // can be contorled by player
 class Controllable : MonoBehaviour { }
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

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

23 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

The best way to create instances of an enemy class 2 Answers

How would I get different references to scripts depending on what object it is attached to? 1 Answer

How can I programatically assign one script as a variable inside another script for use by derived classes? 1 Answer

bind instantiated particles to an object 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