public class TestScript : MonoBehaviour {
private void Main() {
Debug.Log("This method gets called, just like Start!");
}
}
It’s not documented, but there’s sporadic threads and UA questions about it going back to 2014. Like Start, it can be a coroutine. It seems to be called before Start.
Is this a very old legacy thing? Is this something that’s used internally by Unity?
At first look, it seems like it could be an artifact of the C++ backend. But no. C++ uses lower-case main (and that doesn’t auto run in Unity) and must return int, and isn’t a member function. And, now that I think of it, the C# isn’t even translated into C++, is it?
It is if you run IL2CPP, but that’s besides the point. The function won’t be run as a main function.
Somewhere in the internal workings of Unity, the engine looks for MonoBehaviours that define a function named “Main”, add it to a list of MonoBehaviours with the Main method, and runs that method whenever the object becomes active for the first time, before Start.