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 :slight_smile:

Thanks

1 Like

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

+1 for a good question :)

You can add a component "Light" to your camera and increase the intensity

2 Answers

2

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.

I need more details about this. So did the parent class MonoBehaviour 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?

Please think before replying whatever stupidity comes to your mind. This is a mesh preview, not the view of a custom scene.

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.

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

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.

This the "Build settings" I am referring to Main menu>file>build Setting. gives the build setting window to choose what platform you want to make builds for. I only tested WebG vs. Stand Alone & Android. Both stand-alone and the android look better. I find that metals don't change so much matte objects change a lot.