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
5
Question by gribbly · Jan 27, 2013 at 12:36 AM · c#classinheritanceawake

Can I/Should I call Awake() in parent class manually?

I'm having an issue where Awake() in a child class is hiding Awake() in the parent.

I have this:

public class BaseClass: MonoBehaviour {
  void Awake () {
    Debug.Log("BaseClass Awake!");
    //do base class set up stuff
  }
}

..and then the child class:

public class ChildClass: BaseClass {
  void Awake(){
    //do child class setup stuff
  }
}

...but of course Awake() in ChildClass is "occluding" Awake() in BaseClass, so I don't see the Debug.Log, etc. My understanding is that this is the correct C# behavior.

What I want to do is something like:

public class ChildClass: BaseClass { void Awake(){ //make sure base setup is done base.Awake();

 //do child class setup stuff

} }

...but I've hit my limit of C# understanding! Is there a right way to do this? Or should I be looking at the whole situation differently?

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
15
Best Answer

Answer by $$anonymous$$ · Jan 27, 2013 at 12:48 AM

You should use public virtual on you baseclass method and use public override in your inheriting class if you want to override it but be able to access the parent's Awake() too.

baseclass

 public virtual void Awake() { // baseclass stuff }

child

 public override void Awake() { base.Awake(); // and child stuff }

This will call the baseclass's method, then the child's method body. You have to make them public because virtual or abstract members cannot be private, you get a compiler error if you omit the public accessor on any of them.

Another thing you could do is define a virtual void OnAwake() in you baseclass, which is empty, and code your baseclass' Awake in Awake(), then call OnAwake() (or really any method) from the base Awake, like this:

 /// in baseclass:
 public virtual void OnAwake();
 void Awake() {
    // do baseclass awake stuff
    OnAwake();
 }
 
 /// in inheriting class:
 public override void OnAwake() {
    // do subclass stuff
 }

Although base.Awake() has more control since you can specify if it runs before or after (or somewhere between) your inherited class's routine, depending on when do you call it (but for sake of clarity you should write your code so base.anything is the first thing you do in a method). Also if you have multiple levels of inheriting classes, this can become erroneous, so use the base.Awake() approach, because it will execute every parent class's code.

Comment
Add comment · Show 6 · 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 gribbly · Jan 31, 2013 at 06:48 AM 0
Share

Thanks for the detailed response, I really appreciate it.

But I'm having trouble making it work... if I put an Awake() in my inheriting class, I get a compile-time error:

 Unhandled Exception: $$anonymous$$ono.CSharp.InternalErrorException: Assets/Scenes/Chooser/Chooser.cs(5,14): Chooser ---> System.InvalidOperationException: $$anonymous$$ethod 'Chooser.Awake' does not have a method body.

"Chooser" is my inheriting class.

Here's the code in my inheriting class:

 public class Chooser : BerliozScene {

     void Awake(){
         base.Awake();
         Debug.Log("Chooser: Awake");
     }
     

...and so on. Then in the parent class:

 public class BerliozScene : $$anonymous$$onoBehaviour {
     
     void Awake () {
         Debug.Log("BerliozScene: Awake");
        
             //bunch of setup stuff that works fine as long as I don't have an Awake() in Chooser!
     }

...and so on.

Any thoughts?

avatar image $$anonymous$$ · Jan 31, 2013 at 09:44 AM 0
Share

Sorry, my bad. Your initial approach contained some errors. Edited the answer.

avatar image gribbly · Feb 01, 2013 at 07:09 AM 0
Share

Thanks, that cleared it up for me. Really appreciate the clear answer and description.

avatar image zblackmore · Jul 11, 2013 at 11:56 PM 4
Share

Wouldn't it be more correct OO-wise if the base class declared Awake() as protected ins$$anonymous$$d of public?

avatar image Witchunter · Oct 16, 2016 at 09:23 AM 2
Share

I would also strongly advise on changing public to protected modifier. $$anonymous$$aking it public is a very bad OOP practice.

Show more comments

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

11 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

Related Questions

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

C# Conception - Hide inherited members and functions 1 Answer

Behind the scenes voodoo w/ Instantiate 2 Answers

Parent Class variables in Child Class's Inspector (C#) 0 Answers

C# Array of Child Objects 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