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
3
Question by Tdkr80 · Jun 08, 2017 at 03:46 PM · virtualmethodsvirtual functions

How come start and update methods aren't virtual?

Hello everyone,

I was just wondering why (and how) Unity allows me to do the following in C#:

 public void Start()
 {
     // Do something here
 }
 
 public void Update()
 {
     // Do something here
 }

Do the Start and Update methods inherit from MonoBehaviour? Why is it that I don't have to override a virtual method to use Start and Update? Are they virtual methods?

Anyone that can shed some light on this will be praised and loved :)

Thanks

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 Anton-Korhonen · Jun 14, 2017 at 04:21 AM 1
Share

Rember to mark an answer as correct, so other people with the same question find it better:)

avatar image Trevdevs · Jun 28, 2018 at 08:06 PM 0
Share
  • for a good question :)

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Anton-Korhonen · Jun 08, 2017 at 06:01 PM

MonoBehaviour doesn't define the methods at all - they're not part of its interface. Unity uses a programming mechanism called reflection to check for the appearance of these special functions like Start, Update, Awake and so on. Simply put the Unity engine has predefined function naming conventions that it's trying to find in classes that subclass MonoBehaviour.

So you are not overriding anything from MonoBehaviour, but actually providing the functions for it.

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 raining_day0513 · Nov 04, 2020 at 02:45 PM 0
Share

I need more details about this. So did the parent class $$anonymous$$onoBehaviour call "start()", "Update", "Awake"? I mean: so when these functions are called/used?

And the most important one: what's the advantage of "reflection"? Why not just use overriding?

avatar image
4

Answer by TonyLi · Jun 08, 2017 at 06:09 PM

Unity uses reflection into your class to identify any special methods (Start, Update, OnApplicationQuit, etc) that you've defined. It will only invoke those methods. Given the number of special methods that MonoBehaviours support, it would be very inefficient to invoke every single one on every single script if they were all defined as virtual methods, especially since most of them wouldn't be overridden and would just be empty methods providing nothing but unnecessary overhead.

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 TonyLi · Jun 08, 2017 at 06:11 PM 0
Share

Tabbed away from the window for a couple $$anonymous$$utes and got beat to the punch. :-) Anton's answer is good.

avatar image paulozemek · Jun 28, 2018 at 07:52 PM 1
Share

Actually, reflection can detect if a method was overridden or not. So, it would be quite nice if all those methods were actually virtual (so writing override will give us the list of all available methods), and we wouldn't lose anything for not overriding them.

avatar image TonyLi paulozemek · Jun 28, 2018 at 08:15 PM 0
Share

You could take that to feedback.unity3d.com. They just didn't implement it that way. Personally I don't think it's that big a deal.

avatar image Bunny83 paulozemek · Jun 28, 2018 at 09:43 PM 1
Share

While it's true that you could detect if a method is overridden or not, virtual methods are generally slower than non-virtual method regardless of if they are overridden or not. That's because virtual methods are executed through the virtual methods table ( vTable ). So it's generally an indirect method call.

avatar image paulozemek Bunny83 · Jun 28, 2018 at 11:43 PM 0
Share

Virtual methods are slower than non-virtual methods when the virtual dispatch is used (this can be avoided when you seal a class and in many other situations).

Yet, I am sure there's a virtual dispatch already happening (I don't know if they compile a delegate to call the Update method, or how exactly they do the call, but I am sure it is not a static dispatch). So, the only thing that would really change if those methods were virtual in the base class would be that they would be available to any IDE when we wrote "override". They would still get invoked the exact same way they are now, without adding any additional virtual dispatch.

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

9 People are following this question.

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

Related Questions

VRSettings.supportedDevices not populating if None is the first SDK. 0 Answers

Can't Override Virtual Method In Inherited Class 1 Answer

Good practice to do a base extend of MonoBehaviour? 1 Answer

Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do? 0 Answers

why cant I see more than one perameter in gameobjects 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