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 SassMaster · Sep 23, 2014 at 02:06 AM · inheritanceclasses

How to inherit from two classes?

I've already look around for an answer to this, and most answers involve partially cheating monodevelop to inherit two classes and usually causes errors. I'm stuck wondering is there a way to just literally inherit two classes?

I've got an Inventory class which contains functions such as AddItem() and RemoveItem(), and then I have the tile functions class (It's a tile based game), which involves functions like PlantCharge(), UnscrewVent(), TakeHostage() (It's a heist game).

I'm going to need to be able to use a script to say that if I click on a wall while a breaching charge is selected, call functions PlantCharge() to plant the charge, and RemoveItem() to take the charge from the players inventory once he places it. I've inherited from Tile functions, but I can't get the equippedItem variable that I'm using to detect which item the player is holding.

So there, I'm stumped. I'd have expected that Unity would have a method of inheriting from two classes, or, at least I don't know if it has.

Comment
Add comment · Show 1
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 roojerry · Sep 23, 2014 at 07:03 PM 0
Share

What would this class be that would inherit from both an inventory and a tile? I think your design is a bit flawed. I would think that the player class would have an instance of an inventory and a reference to the current tile the player was standing on. That way, when handling the input for the player, you would remove from the players inventory and place an object on the current tile the player is on. No object should contain both the functionality of a tile and an inventory at the same time (well, at least not in your case).

2 Replies

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

Answer by Kiwasi · Sep 23, 2014 at 02:10 AM

There is no way to inherit from more then one class.

What you can do is implement an interface, which is pretty close.

Another option is to use reflection to figure out what methods are available. I would advise against this unless you have no other option. In many cases reflection can be avoided by using the right structure.

Comment
Add comment · Show 5 · 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 SassMaster · Sep 23, 2014 at 05:53 PM 0
Share

What do you mean by "Suing"?

avatar image gjf · Sep 23, 2014 at 06:52 PM 1
Share

That's most likely a typo: using

avatar image Kiwasi · Sep 23, 2014 at 07:28 PM 0
Share

Typo fixed

avatar image SassMaster · Sep 23, 2014 at 09:18 PM 0
Share

Alright. I'm gonna try to implement one of these "Interfaces".

avatar image SassMaster · Sep 24, 2014 at 05:12 PM 0
Share

The interface worked perfectly. Thanks.

avatar image
2
Wiki

Answer by Habitablaba · Sep 23, 2014 at 09:37 PM

The inability to inherit from two classes at once is not actually related to Unity at all, but a 'limitation' of the programming language.

There are actually quite a few ways to accomplish what you are asking for, but I think you may not be trying to solve this problem in the 'best' way possible. You are coming at this from an Object Oriented standpoint, which often times will find you in uncomfortable places with Unity.

Instead of thinking in strictly IS-A and HAS-A relationships, like you might in an OOP environment, think more in terms of how each object is meant to behave. Remember that each GameObject can have more than one script attached to it, so you you could have a script simply to manage the player's movement as well as a script to handle the placement of explosives or unscrewing of a vent.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling a method of a class that is a part of another class? 1 Answer

I have objects that have the same set of variables, but different desired behaviors. How can I do this without repeating variable declarations across all behaviors? 1 Answer

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

Polymorphism question for my custom class 1 Answer

Inheriting method that uses an instance property 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