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 /
avatar image
0
Question by piotrzulawski · Sep 14, 2020 at 10:57 PM · inheritancepolymorphism

Inheritance structure

Hello all,

I have some difficulties managing inheritance in C#. I'm writing a game in which the player builds all sorts of machinery; this can be anything, from relatively simple objects (such as simple containers/boxes) all the way to something more elaborate; let's use a coal extractor as an example. I want to inherit certain behaviours from my simple objects in the more complex objects. To give a concrete example:

The coal extractor needs two basic behaviours:

  1. The behaviour of a container (to store the extracted coal, and also so that the player can interact with it in the same way as it does with a box and move the coal from the extractor's internal storage into the inventory)

  2. The behaviour of an electrically powered device (to be able to interact with the electric network, to know whether there is sufficient power in the electric grid etc.)

I hae two classes, Container and ElectricSink, which would do just the job, but C# won't let me inherit from two classes. Moreover, I not only need the behaviour (implementation of methods) from the those two classes; I need my Extractor to actually pass is-a type tests. This is because when the player interacts with an object, the type of interaction gets chosen by a piece of code that has different behaviours based on type. So, when I want to take the coal out of the extractor, I need it to go through a is-a Container test.

This can't be achieved with the interface trick, where you store instances of objects that implement different interfaces, because the underlaying class holding those objects still cannot itself pass the type testing.

Any ideas as to how I can solve this issue? Thanks!,

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
2
Best Answer

Answer by Hellium · Sep 15, 2020 at 07:21 AM

Unity encourages you to adopt the Composition over inheritance principle and this is clearly the solution to your problem. Keep the two classes but don't try to make one inherit the other.


Keep your container class, attach it to your object and make your CharcoalExtractor class not inherit from Container but just use it. Same for your electrical powered class.


EXAMPLE:

 [RequireComponent(typeof(Container))]
 [RequireComponent(typeof(ElectricMachine))]
 public class CoalExtractor : MonoBehaviour
 {
     private Container container;
     private ElectricMachine electricMachine;
     
     void Awake()
     {
         container = GetComponent<Container>();
         electricMachine = GetComponent<ElectricMachine>();
     }
     
     void Update()
     {
         if(electricMachine.IsPlugged)
              container.Add(new Charcoal());
     }
 }
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
0

Answer by N-8-D-e-v · Sep 15, 2020 at 12:41 AM

I didn't read everything, but you can use an interface for this, as opposed to inheriting from two classes (which is impossible in c#)

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

138 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 avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

How to change variables from another script? 1 Answer

How can I access an inherited method from a separate (collided) object? 1 Answer

Calling methods on inherited classes in C# 2 Answers

Derived Class Fields 3 Answers


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