Execution of Main Function in Monobehaviour

Do You know Unity calls Main() method in monobehaviour like Start()

.

In Monobehaviour ,if you write parameterless Main() method with void return type then unity will call this main method after OnEnable() and before Start() method. Main is called before the first frame update only if the script instance is enabled.

It also allows access of unity objects in Main() method.

Here is a code snippet,

public class MainMethodExample : MonoBehaviour
{
    void Awake ()
    {
        Debug.Log ("Awake");
    }

    void OnEnable ()
    {
        Debug.Log ("OnEnable");
    }
    
    void Main ()
    {
        Debug.Log ("Main");
    }

    // Use this for initialization
    void Start ()
    {
        Debug.Log ("Start");
    }
}

Is it good to use Main() in Monobehaviour as unity is not mentioning it in Unity Documentation?

1 Like

Main is used in console or windows c# apps as the entry point. I don’t think it’s a Unity ā€œmessageā€ as Visual Studio list the other methods, which is why they probably don’t mention it. I’m sure someone a lot more knowledgeable than I can chip in. I’d be curious if it ran outside the editor the same way.

It’s actually an interesting fact. Though even if it’s called by the engine, I wouldn’t rely on it as long as it’s not officially documented.

Other than that, a quick test has shown that it runs Main&Start immediately one after the other per instance, so the code in Main could simply run first in Start and it’d save that call to Main (unless extensive testing reveals special behaviour).
And if really something like RunAfterAllAwakes / RunBeforeAllStarts is needed, an event would do it’s job as well.

Anyway, if anyone has done some more tests regarding that, I’d be interested as well. :slight_smile:

1 Like

Never rely on undocumented features, since their implementation might change without any notice.

4 Likes

i checked in mobile device.it gives same result.

That’s weird. It might well be some strange historical feature. I wouldn’t use it at all.

If it’s historical, it might be removed randomly.

If it’s meant to be there, Unity is probably using it for something special that you don’t want to mess with.

Also, that ā€˜Main’ method could be run as a coroutine. In my opinion, because of previous fact, it calls by the engine, not as entry point of c# program.

1 Like

In C# Applications the Main method lies in a special class (Program) which is called in order to start the application.
In Unity this entry point of the application is hidden in the engine code (I wished we could have a method for initializing the app also, but it doesn’t exist).

Since this Main() method here is just another ā€œmagic-methodā€ of a MonoBehaviours, and it can be created for several MonoBehaviours and it is not called first, this has nothing to do with the entry-point-main-method.

I would not use it as others already told. It might be deleted in future versions of Unity and you can achieve everything you need with the other methods you showed.

1 Like

Disabled in 2020.1.

3 Likes