Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dalmar · Jun 21, 2016 at 09:16 PM · objectclasses

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.

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
1

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".

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 Dalmar · Jun 21, 2016 at 10:12 PM 0
Share

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.

avatar image fafase Dalmar · Jun 22, 2016 at 05:36 AM 0
Share

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.

avatar image Bunny83 Dalmar · Jun 22, 2016 at 10:47 AM 0
Share

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;
avatar image Dalmar · Jun 22, 2016 at 03:33 PM 0
Share

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!

avatar image Dalmar Dalmar · Jun 22, 2016 at 03:36 PM 0
Share

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

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

46 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 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 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

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


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