Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by Aggressor · May 11, 2015 at 02:20 AM · c#inheritancestart

Parent Start() not being called from base.Start()

I have very straight forward code but I have no idea why my base.Start() is not being called.

Call 1 is logged, Call 2 is not. Can you advise why base.Start() is not calling the parent class?

 public class AggressiveBruiser : Personality 
 {
 
     void Start () 
     {
         Debug.Log("Call 1");
         base.Start();
     }
 }
 
 public class Personality : Enemy 
 {
 
     void Start()
     {
         Debug.Log("Call 2");
         base.Start();
     }
 }
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

2 Replies

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

Answer by Bunny83 · May 11, 2015 at 03:32 AM

Your Start method isn't a virtual method and you're not overriding it in the derived class. That means you just hide the method in the base class.

  public class AggressiveBruiser : Personality 
  {
  
      protected override void Start () 
      {
          Debug.Log("AggressiveBruiser: Start");
          base.Start();
      }
  }
  
  public class Personality : Enemy 
  {
  
      protected override void Start()
      {
          Debug.Log("Personality: Start");
          base.Start();
      }
  }
 
  public class Enemy : MonoBehaviour
  {
  
      protected virtual void Start()
      {
          Debug.Log("Enemy: Start");
      }
  }

The method of course need at least protected visibility to be able to override it. Public or internal would work as well.

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 Aggressor · May 11, 2015 at 03:33 AM 0
Share

I think thats Ugly :( I just made my own Init class and called it from there. Its Ugly because Start() isn't even listed as protected. But thank you this clears it up.

avatar image Bunny83 · May 11, 2015 at 03:46 AM 0
Share

Why is that ugly? That's how inheritance works... If a method isn't virtual you can't override it since there's no v-table in the class.

avatar image Aggressor · May 11, 2015 at 03:47 AM 0
Share

As in Start is not shown as protected Start() it just appears as Start().

avatar image Bunny83 · May 11, 2015 at 04:11 AM 2
Share

Unity uses reflection to call any of it supported callbacks. Because of that it doesn't matter if it's declared as private, protected or public. That's why it's per default declared as private. Important: The $$anonymous$$onoBehaviour class doesn't declare a Start method which could be overridden. Since Unity uses reflection to call those callbacks it can optimise the callbacks. Unity only calls methods that actually have been declared in the class.

Actually what Unity does is "ugly" in the sense of OOP. Protected is like private but allows derived classes to see and override them. Private methods can't be overridden since they are private. No other class can see / use / override them except the own class.

That's all just common OOP. If a class declares something as private it indicates that it doesn't want a derived class to access it. If a derived class should be able to access a member it has to be at least protected. That's also true for variables.

avatar image Aggressor · May 11, 2015 at 04:14 AM 0
Share

Thats worth a beer!

avatar image
0

Answer by phxvyper · May 11, 2015 at 03:33 AM

By default, MonoBehaviour hides inheritance.

You need to define your higher Start function as virtual, so that the base.Start function may be called.

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

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

22 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

Related Questions

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

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Derived Class Fields 3 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