- Home /
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 :)
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.
Your answer
Follow this Question
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