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 BenKurdziel · Sep 26, 2013 at 01:58 AM · multipleinheritancemonobehaviour

MonoBehaviour with Multi-Inheritance

Hey everyone. So I understand C# doesn't directly support Multi-Inheritance, though I know there are ways around it. I've successfully implemented multi-inheritance for 2 different classes, however I'm continually finding an issue with linking MonoBehaviour in as well.

Copied from an email asking another person:

I can link both Photon and the grid system (TMN Controller) in fine, and the script will pull inheritance from both without any problems. However, when I add MonoBehaviour (required to access things like gameObject, or Destroy), it seems as if it's not recognizing it at all. If I make the class inherit from MonoBehaviour as normal, the errors go away, but I lose the inheritance to Photon and TMN. If I have any combination of TMN + MB or Photon + MB, it still doesn't work, so I know the problem isn't with having too many things linked, but rather with linking MonoBehaviour itself.

This is my code for that section:

 public interface TMN { TMNController myController {get; set; }}
 public interface Pho { Photon.MonoBehaviour myPhoton {get; set; }}
 public interface Mon {Behaviour myMono {get; set; }}
 
 public class gameSetUp : TMN, /*Pho,*/ Mon
 {
     public TMNController myController {get; set; }
     public Photon.MonoBehaviour myPhoton {get; set; }
     public Behaviour myMono {get; set;}
 
     public gameSetUp() { myController = new TMNController(); myPhoton = new Photon.MonoBehaviour(); myMono = new Behaviour();}
     
 }
 
 public class script_SetUp : gameSetUp {
 
 }
 

I have 3 errors, all referring to missing references related to MonoBehaviour. I tried linking every combination of Component, Object, GameObject, UnityEngine classes, and nothing changed the errors.

My 3 Errors are:

1) Assets/Scripts/script_SetUp.cs(71,30): error CS1061: Type script_SetUp' does not contain a definition for gameObject' and no extension method gameObject' of type script_SetUp' could be found (are you missing a using directive or an assembly reference?)

2) Assets/Scripts/script_SetUp.cs(80,23): error CS0029: Cannot implicitly convert type script_SetUp' to UnityEngine.Component'

3) Assets/Scripts/script_SetUp.cs(82,22): error CS1061: Type script_SetUp' does not contain a definition for photonView' and no extension method photonView' of type script_SetUp' could be found (are you missing a using directive or an assembly reference?)

The script seems to have trouble recognizing itself. It think it's a Component, not a "script_SetUp"

The script also doesn't show up in my inspector. Just a message saying "The associated script can not be loaded. Please fix any compile errors and assign a valid script."

Is there some special way this needs to be handled for MonoBehaviour, is my logic wrong? I'm really not sure at all about how to go about this.

Any help would be greatly appreciated. If you need any other information, feel free to ask and I'll provide you with what you need. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Sep 26, 2013 at 03:24 AM

Consider carefully whether you should use constructors in Unity - you are advised to use Awake instead - reason being, all of you stuff will be overwritten by the serialization process that happens immediately after construction for everything but new objects.

Secondly - public class gameSetup needs to be defined like this:

  public class gameSetUp : MonoBehaviour , TMN, /*Pho,*/  Mon
  {
  }

Otherwise it's just a custom class.

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

16 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

Related Questions

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

What is the most elegant solution to replace multiple inheritance for this? 0 Answers

Polymorphism question for my custom class 1 Answer

How to add default behavior of OnDestroy function for all MonoBehavior scripts? 1 Answer

Serialization and Inheritance: Great-GrandChild Class appears unserialized 0 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