• 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
Question by Maulkye · Jul 31, 2010 at 03:13 PM · method

C# Overridable Methods

Maybe this is a silly question, so apologies up front if it is.

If some methods (say FixedUpdate) are overridable methods, why don't I see their virtual declarations anywhere in the metadata for the hierarchy chain (MonoBehaviour > Behaviour > Component > Object)?

Comment
Ricardo
vexe
Moix

People who like this

3 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Mike 3 · Jul 31, 2010 at 03:18 PM

They aren't actually overridable. What happens is the engine uses reflection to find them all in your scripts at creation, then invokes the stored references at runtime

It also means that scripts that don't have an Update (or any other) method don't need to have it called at runtime - that means it's a bit faster as it doesn't have to call every function in every script from the native engine

Comment
Tetrad
qJake
Ricardo
Maulkye
Bunny83
BigRoy
wcoastsands
obiolg
ThinhHB
JoeyDeVries

People who like this

10 Show 2 · 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 qJake · Jul 31, 2010 at 06:26 PM 2
Share

Not to mention you don't need to call base.Update(); either ;)

avatar image NateJC · Aug 14, 2015 at 09:56 PM 0
Share

You say that calling base.Update(); is unnecessary. But what if I want to control if and when it calls base.Update();? Is this not possible?

avatar image
Best Answer

Answer by Lucas Meijer 1 · Jul 31, 2010 at 11:05 PM

This is a bit of a design mistake on our end. They are called trough reflection, so we only call the methods on objects that actually have the method. We should have made the methods be virtual in the MonoBehaviour basetype though, so you get nicer autocompletion etc. I would love to correct this situation, but the problem is that if we do, we would break pretty much all c# scripts out there, as you'd have to rewrite

void Update()
{
}

to

override void Update()
{
}

maybe we'll do it one day when we have the capability to automatically make changes like these for you, but that day is not in the near future

Comment
qJake
Maulkye
BigRoy
wcoastsands
Moix
ThinhHB
JoeyDeVries

People who like this

7 Show 17 · 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 Mike 3 · Jul 31, 2010 at 11:45 PM 1
Share

If they were abstract, they'd have to be implemented though. All of them. That'd make people either get very annoyed at having to implement them every time, or force them to write their own base script which implements all of them as virtual. Either way, it would negate any ability to skip updating functions for speed

avatar image Lucas Meijer 1 · Aug 01, 2010 at 09:17 AM 0
Share

Hey Mike, thanks, I fixed the post to say abstract->virtual. It would not negate any ability to skip updating for speed, as we could still use reflection to see which classes do override the method, and which don't.

avatar image Mike 3 · Aug 01, 2010 at 10:13 AM 0
Share

Indeed, with virtual methods. With abstract (which I was complaining about), not a chance :D

avatar image Maulkye · Aug 03, 2010 at 08:58 PM 0
Share

Ahh, ok. Thanks!

avatar image jc_lvngstn · Sep 27, 2012 at 07:05 PM 1
Share

No advantage of virtual methods over reflection? I would think performance would be one. Another very real advantage is, when you use Visual Studio (especially with Resharper), you can very quickly see what methods you can override.

It's annoying to have to go back to the scripting reference just to see what that mouse over enter/exit method name is.

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Call Function in unknown Class 1 Answer

Can't override GetHashCode (cannot change return type when overriding method int UnityEngine.Object.GetHashCode") 1 Answer

how to call the script of an object being collided with a trigger2D 1 Answer

Alternative for array.contains() method 1 Answer

Execute different method set in inspector 0 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