Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Nerethar · Jan 18, 2013 at 12:16 AM · c#inheritanceclasses

Won't excecute the overwrited function

Hi guys, I've got a little problem with custom classes and inheritance.

I've got an empty base class, for example

 class A{
 
     public void Acoolfunction(){
        //do nothing
     }
 }

but now I've made some other clases

that inheritate from this class. Lets give it an other example:

 class B : A{
 
     public void Acoolfunction(){
        Debug.Log("Hello World");
     }
 }
 
 class C : A{
 
     public void Acoolfunction(){
        Debug.Log("Cya World");
     }
 }

Now we come to the tricky part:

I need a list that can contain both classes, B and C. So I created an array of A and added B's and C's to it. My problem is, that when I try to call the Acoolfunction of my listed classes, he will allways call the function in A, not the functions from B and C that overwrited it.

So I know the problem, but if I remember right it should have worked that way :/ How can I get it to actually call the right method and not the parent typ method?

Thanks for any help.

Comment
Add comment · Show 2
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 Bunny83 · Jan 18, 2013 at 12:31 AM 1
Share

btw. in program$$anonymous$$g we don't use the word overwritten (from overwrite) because the function is overridden (from override) which means bypass / replace in this context and not literally overwritten ;)

avatar image Nerethar · Jan 18, 2013 at 12:47 AM 0
Share

Ahh, good to know, I am from germany and here we say "überschrieben" what means overwritten and "ersetzt" what would be replaced, it kinda depends who is talking to you, it seems everyone uses the word he likes here^^ But thanks, now I know at least how to say it correctly in english =)

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by whydoidoit · Jan 18, 2013 at 12:23 AM

You need to specify that Acoolfunction is virtual and then override it in the subclasses:

 class A {

          public virtual void Acoolfunction() {
          }
       
 }

 class B : A {
       public override void Acoolfunction() {
              Debug.Log("Hello world");
       }

 }
Comment
Add comment · 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
1

Answer by liszto · Jan 18, 2013 at 12:23 AM

If your first class A do nothing why don't do this :

 class A{
 
     public virtual void Acoolfunction(){
        //do nothing
     }
 }

then this for the others :

 class B : A{
 
     public override void Acoolfunction(){
        Debug.Log("Hello World");
     }
 }
 
 class C : A{
 
     public override void Acoolfunction(){
        Debug.Log("Cya World");
     }
 }

I think this can work.

Comment
Add comment · 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
0

Answer by Nerethar · Jan 18, 2013 at 12:44 AM

Wow, 2 correct answers at the same time, it was a hard choice, who gets the mark for the right answer^^ Thank you both very much, now it is working. Sadly, for object based languages I'm just used to Java, where you can simply name the function the same way to override it^^

Good night guys and thanks again =)

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 whydoidoit · Jan 18, 2013 at 12:46 AM 0
Share

You can (legally) overwrite the function by using the new keyword - but then as you saw, it doesn't make it virtual, it just lets you redefine it which can be useful (but not for what you are doing!)

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

12 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

Related Questions

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to choose base class to make Inherit C# 2 Answers

Calling a method of a class that is a part of another class? 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