I have been trying to understand inheritance in C#. I have searched lots of materials for this. Including docs.microsoft.com and unity3d.com and docs.unity3d.com. I understood that if we inherit from other classes we will have the all futures of the inherited class (parent class). When our class inherits from another its called child or derived class. And Lets say this derived class name is B and inherits from A. If we create another class called C and inherit from B this class will have the both functionalities of both A and B. I also learned which modifiers will be accessible and which are not. Everything till now is just so clear. But I tried to understand Why do we inherit from MonoBehaviour and I took a look at docs.unity3d.com MonoBehaviour. There I took a look at all of the classes in UnityEngine namespace. All of the classes inside there were either classes in UnityEngine or structs. Then I learned that structs are like classes but more limited also they they dont support Inheritance but can implement Interfaces. These are ok too. When it comes to classes I saw that: they dont inherit from anything, inherit from Object, Inherit from Behaviour or Inherit from Component. But nothing inherits from MonoBehaviour which makes sense because it should be the smallest child to have all of the functionality of its parents. So the logic is Mono inherits from Behaviour which inherits from component which inherits from object and Object is just a class that doesnt inherit from anything else So the 1st parent item. There are also classes which doesnt inherit from anyhting like Object class e.g microphone or compass etc. So I thought I could create a class which doesnt inherit from Monobehaviour but using the UnityEngine namespace and have no problem at all if I use classes that has nothing to do with MonoBehaviour. And it works correctly until This point :

 using UnityEngine;

// If I inherit from Component here it allows me to use gameObject but if I use like Component.gameObject it doesnt 
// allow me. Why? This is another question I have.
public class MusicPlayer

void Example ()
    // Lets say I want to use a class which inherits from Behaviour which is Animator
    // After putting . It only shows inherited members i cant access its own members
    // I dont know why. I wonder why it only shows inherited members ?
    // And second question I know I cant call a class like this but how would I do this Whats the proper way? Without 
     // inheriting from MonoBehaviour
    // Im so lost xD I was trying to learn all the aspects from start but i kinda stuck now : D

   // Although if use a class doesnt inherit from anything like Mathf it works perfectly 
   // I can reach its own members (it doesnt have any inherited member as expected )


I probably made this context sooo complicated : D If anything is unclear pls ask me what I mean.
I will really be greatful to those who tries to help me out with this. Thanks in advance

Wow I think I solved it. It was all about Statics. Now everything started to make sense. But I would still love to see an answer to this since Im not hundred percent sure if it was the statics. Thanks

Privacy & Terms