• 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 c$$anonymous$$ld class is $$anonymous$$ding Awake() in the parent.

I have t$$anonymous$$s:

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

..and then the c$$anonymous$$ld class:

public class C$$anonymous$$ldClass: BaseClass {
  void Awake(){
    //do c$$anonymous$$ld class setup stuff
  }
}

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

What I want to do is somet$$anonymous$$ng like:

public class C$$anonymous$$ldClass: BaseClass { void Awake(){ //make sure base setup is done base.Awake();

 //do c$$anonymous$$ld class setup stuff

} }

...but I've $$anonymous$$t my limit of C# understanding! Is there a right way to do t$$anonymous$$s? 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 }

c$$anonymous$$ld

 public override void Awake() { base.Awake(); // and c$$anonymous$$ld stuff }

T$$anonymous$$s will call the baseclass's method, then the c$$anonymous$$ld'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 t$$anonymous$$ng you could do is define a virtual void OnAwake() in you baseclass, w$$anonymous$$ch is empty, and code your baseclass' Awake in Awake(), then call OnAwake() (or really any method) from the base Awake, like t$$anonymous$$s:

 /// 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.anyt$$anonymous$$ng is the first t$$anonymous$$ng you do in a method). Also if you have multiple levels of inheriting classes, t$$anonymous$$s 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
avatar image $$anonymous$$ · Jan 31, 2013 at 09:44 AM 0
Share
avatar image gribbly · Feb 01, 2013 at 07:09 AM 0
Share
avatar image zblackmore · Jul 11, 2013 at 11:56 PM 4
Share
avatar image Witchunter · Oct 16, 2016 at 09:23 AM 2
Share
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Behind the scenes voodoo w/ Instantiate 2 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