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
0
Question by Macro · Sep 01, 2014 at 08:20 AM · c#monobehaviour

Why does MonoBehaviour not allow message functions to be overridden?

I constantly forget what the message functions names are when working with Unity, i.e Awake, Start, OnEnable etc. So for 20 minutes the other day I had an OnEnabled function and was wondering why it was not being invoked, whereas it should have been OnEnable, however there is no interface or virtual method indicating that you should override it if you want to be able to hook into these events.

So I was just wondering is there a particular reason why these methods are not virtual or at least already defined in some way for you to know what they are and override them?

I will assume they are not virtual because of the overhead of virtual methods, and they cannot be abstract because then you are forced to implement them, and finally are not on an interface and empty because then you would need to do some funky new function over the top of the other.

It just seems that there should be a better way to do this without me needing a cheat sheet to see what methods unity will allow me to hook into, especially as c# provides such good intellisense these days.

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
0

Answer by kacyesp · Sep 01, 2014 at 08:45 AM

What do you mean "there is no interface" ? Does the documentation for Monobehaviour not count? http://docs.unity3d.com/ScriptReference/MonoBehaviour.html

You're right about not using virtual methods to reduce needless overhead, and that's why Unity uses reflection, which is just a fancy way of saying that Unity only calls on the methods if they are implemented. If they aren't implemented, then they aren't called at all.

Comment
Add comment · 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 Macro · Sep 01, 2014 at 08:57 AM 0
Share

Well yes there is documentation, however when you open up in your IDE and start off with your monobehaviour class, you either have to recall from memory what the message names are or load up the documentation, and there is no compile time safety around this. This is my worry, as listed above a typo wont be caught so your code wont work and you are open to human error.

The interface comment was around if they had an I$$anonymous$$onoBehaviour$$anonymous$$essages then at least you could not have virtuals and just overwrite the method functions defined on the interface, but at least you would SEE the available functions then. Also reflection is DREADFULLY slow, if not slower than virtual calls, although I presume they do it in some way making it more efficient.

Anyway my ultimate concern is that I HAVE to currently have a list of the available unity methods available to enter and I have no compile time/intellisense safety around this.

avatar image kacyesp · Sep 01, 2014 at 09:12 AM 0
Share

@$$anonymous$$acro Oh I see what you're saying now. Well if you're really concerned about this, for now you could always implement your own interface and let the guys at Unity know you want compile time safety: http://feedback.unity3d.com/ I agree that overriding the message functions would be nice.

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

23 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

transform.position reads 0 but the actual position is different 1 Answer

Enabling multiple Monobehaviour Components in a game object 1 Answer

Is there any way to run a class script from visual studio? 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