Get the base class from the derived class
Hello, I am having problems with this all day, I found something like this: http://stackoverflow.com/questions/19243057/how-to-get-base-class-instance-from-a-derived-class but did not help. The problem is:
I have the class "Character" and class "Player" is a derived class from character.
I need to pass the "Character" as class as parameters to a method ( I need to pass it inside Player class). How can i get this info? I already tried GetComponent(); I already tried to make the Character class return itself, I already tried the getType().BaseType(), but it only returns the base type (and i need the class, not type).
Thank you.
Answer by Bunny83 · Jun 21, 2016 at 09:27 PM
It seems you haven't understood what inheritance actually means, If the Player class is derived from Character, then a Player instance is also a Character. Just like a particular person is a human and every human is a living being.
If your Character class is derived from MonoBehaviour the inheritance chain is even longer.
System.Object
/|\
|
UnityEngine.Object
/|\
|
Component
/|\
|
Behaviour
/|\
|
MonoBehaviour
/|\
|
Character
/|\
|
Player
A Player instance is also a Character and is also a MonoBehaviour and is also a Component. Treating a Player instance as one of it's base classes is called "upcasting". Upcasting happens automatically.
So for example if a method expects a Character as parameter you can also pass a Player instance as a Player is also a Character.
Best example is Unity's Destroy method. It takes a UnityEngine.Object as parameter, that's why you can pass any class that is derived from that Object class.
If you want to use access the object instance of a class itself you just have to use "this".
First of all, thank you for your answer.
I understand about inheritance, but the problem is that the method that I am calling return me all the methods that exists on the class that I pass as parameter.
When I pass Player or this, it returns only the methods of Player and not Character's ones.
I already try to put Character in scene and call from it.. and it return the Characters method.
I need to call it from Player getting the base class.
Player contains the method of Character, at least the public (and protected) ones. If you wish to go to the Character level you can cast:
Character c = (Character)myPlayer;
Character c = myPlayer as Character;
The fact you have a method in Player that requires to pass a Character tells me you may have a wrong design. I can't see why you would need this kind of pattern, if you need to pass a Character to a method within Player, just pass itself since Player is a Character. If you need to see a method from Character in Player, then either you can already if public or protected but you won't see private methods, as it is the point of private. Then you need to change the access.
Exactly like @fafase said, you most likely have strange design. All members of Character also exists in Player. Only public and protected members are visible / accessible (but private members are also there).
You should have included the relevant part of your two classes in your question. Is the method in question a virtual method? Is it overridden in the Player class? The information you've provided is not enough to fully understand your problem.
btw: you don't even need an explicit cast to treat your player instance as Character:
Character c = myPlayer;
I see your point.
Acctually, I am using a plugin, and I contact the owner and told him my problem.
The problem was not in my code, he told me to add something on his code and it worked! Now I can see all methods of the Character class =).
To you know, all the methods that I needed to check were public or protected.
Thank you for helping me!
Oh, and it also worked when i made virtual and override methods, but it was not the way I needed, now its working as it should! =)
Your answer
Follow this Question
Related Questions
How to make objects without the new keyword 0 Answers
NullReferenceException : Object reference not set to an instance of an object? 0 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers
C# Find specific object by getting one of its variables 0 Answers
Passing in objects to class constructor 0 Answers