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 /
avatar image
0
Question by Fyxe · Nov 09, 2016 at 06:21 PM · c#unity 5listclasspolymorphism

Can't call derived class function from list of base class. Ideas?

I've looked all over but cant find anything about how to fix this. How do I get the Foo function to call the overridden function from derived?

 class Base : MonoBehaviour{
      public virtual void Foo(){
           //Something
      }
 }
 
 class Derived : Base{
      public override void Foo(){
           //Something else 
      }
 }
 
 class BaseController : MonoBehaviour{
     List<Base> L = new List<Base>();    // This is filled with bases and deriveds
 
     void Update(){
         foreach(var i in L){
             i.Foo();    // Only calls foo from base class
         }
     }
 }

Ideas?

EDIT: Fixed typos

Comment
Add comment · Show 5
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 Sergio7888 · Nov 09, 2016 at 06:25 PM 0
Share

you need use class Derived : Base you are using class Derived : $$anonymous$$onoBehavoir

avatar image Fyxe Sergio7888 · Nov 09, 2016 at 06:55 PM 0
Share

Sorry, I do have that. Fixed it above, still wont call like I want it to though.

avatar image Sergio7888 Fyxe · Nov 10, 2016 at 09:35 PM 0
Share

check if i is really Derived and not Base only using a Debug.Log(i.GetType().Name).

avatar image doublemax · Nov 09, 2016 at 06:27 PM 0
Share

BTW: You probably want "$$anonymous$$onoBehaviour", not "$$anonymous$$onoBehavoir"

avatar image Bunny83 · Nov 11, 2016 at 01:33 AM 0
Share

I don't even need to test this as i know it will work, at least as you written your example here on UA. If it doesn't work for you, there has to be something different in your actual code.

$$anonymous$$eep in $$anonymous$$d that:

  • each $$anonymous$$onobehaviour class need to be placed in a seperate file with the filename matches the classname

  • those classes should be public. (when you simplify your code for your question make sure it actually represents your case. You seem to have many "typos" which doesn't help to find your problem)

  • the method in the base class has to be either virtual or abstract in order to be able to be overridden. Likewise you have to use the "override" keyword in the derived class.

So there's no problem in the code you (currently) posted so there's (currently) nothing to answer here.

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Landern · Nov 09, 2016 at 06:27 PM

Derived isn't inheriting from Base, you're deriving from MonoBehaviour, it should look like:

 class Base : MonoBehaviour {
       public virtual void Foo(){
            //Something
       }
  }
 
 // Derived should inherit from base which also inherits from MonoBehaviour.
  class Derived : Base {
       public override void Foo(){
            //Something else 
       }
  }
  
  class BaseController : MonoBehaviour {
      List<Base> L = new List<Base>();    // This is filled with bases and derives
  
      void Update(){
          foreach(var i in L){
              i.Foo();    // Only calls foo from base class
          }
      }
  }

Comment
Add comment · Show 1 · 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 Fyxe · Nov 09, 2016 at 06:55 PM 0
Share

I actually do have this I just typed it wrong, sorry

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Requesting help utilizing resources.load to auto assign rigidbody variable in a class list 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to bind different type of data in an array ? 1 Answer


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