Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 CommonDeveloper · Feb 18 at 02:25 PM · inheritanceclasses

Alternative inheritance

Hi all, I’m new in Unity. I have a question about inheritance and prefab management.

In my game i want to use same character as player or enemy: for example I can choose a wizard as player and fight against orcs, that are enemies. In the following match, to fight the new villains, I want to change character and replace my wizard with an orc.

I created two classes: PlayerBehaviour and EnemyBehaviour.

Every character has his own class, like wizardBehaviour, orcBehaviour, goblinBehaviour…etc…, and has to inherit from PlayerBehaviour or EnemyBehaviour.

I created two versions of same script: wizardEnemy and wizardPlayer (that are identical) and in my resources folder I have two copies of the same character, one with attached the “player version” and the other one with “enemy version”. it seems to me that it is inefficient, so, is there another way to do it? Is there a way to make my characterBehaviour inherit from EnemyBehaviour, if I want to use the object as an enemy, or PlayerBehaviour if I want to use the object as player without creating a lot of copies of the same thing?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Shrimpey · Feb 18 at 03:23 PM

There are several ways to deal with this, I'm not gonna post exact answer, but give you some direction for further research. What you've stumbled upon is called "Diamond problem in C#" and is usually solved by using interfaces instead of classes.

Another approach would be to rethink your whole structure and maybe use two classes instead of one, where one of them has an object of the other class inside. It really depends on methods and variables that you want to have there.

Hope that helps at least a bit in further research :)

Comment
Add comment · Show 3 · 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 CommonDeveloper · Feb 18 at 05:04 PM 0
Share

thanks a lot for the ideas!
I have deepened the discourse of the interfaces but I don't understand how I can use them to solve this problem. Since I can only declare methods, without being able to define them, I would still need two classes in which to define the methods, called "PlayerBehavior" and "EnemyBehavior".
The classes of the various characters must have one of the two previous behaviors and add specific functions for each character, so using the interfaces I would still have to define the methods every time. I am not very familiar with interfaces and how they work, How could I use them in this case?

avatar image Shrimpey CommonDeveloper · Feb 18 at 06:11 PM 1
Share

If you have lots of methods to inherit, then I think that in your case the most suitable solution would be something like this (still using classes):

 > Abstract Behaviour class <- PlayerBehaviour and EnemyBehaviour inheriting from that class.
 > Abstract FighterType <- WizardType, OrcType, etc. inherit from that.

And then in your Abstract Behaviour you have variable of type FighterType that defines which type is currently selected. You can then have something like this:

 public abstract class Behaviour{
     protected FighterType fighterType;
     [...]
     protected virtual Move(){}
 }

And then specify different behaviour for enemy and player

 public abstract class PlayerBehaviour : Behaviour{
     [...]
     protected override Move(){
         fighterType.Move(Input.MousePosition());  // This is different Move(), it would be in FighterType class definition and overriden in WizardType, OrcType, etc.
     }
 }
 public abstract class EnemyBehaviour : Behaviour{
     [...]
     protected override Move(){
         fighterType.Move(new Vector3(1f,0f,0f));
     }
 }

This way you will be able to specify "fighterType" movement independently from Behaviour. You can then use different fighterTypes like wizard/orc in that class that will perform different things under the hood. You could also use Scriptable Objects for that so that you could predefine variables for each fighterType in inspector and just drag them onto movement script (enemy or player one).

avatar image CommonDeveloper Shrimpey · Feb 18 at 09:53 PM 0
Share

Thank you for your help, I think I will use this approach, it seems to me very clear and efficient!

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

135 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 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

Inheriting from classes from other files (Javascript) 1 Answer

How to make an array inherit from a class? 2 Answers

Getting the same variable from two different classes? 2 Answers

Defining and inheriting from a Javascript Class 0 Answers

Class extends Object but does not inherit variables 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