- Home /
Monobehaviour IEnumerator Main() getting called automatically
I created a coroutine called "Main" on my Monobehaviour, and Unity seems to call it between Awake() and Start(). I can't find any reference to this behaviour in the Unity docs.
Can I count on this behaviour? Is this functionality deprecated? Did I miss it in the docs?
I think more information is needed. Where do you actually start the co-routine? if its in Awake I could see this happening.
Can you show us the code please?
I don't know anything about Unity running $$anonymous$$ain functions. If you rename the function to "Not$$anonymous$$ain", what happens? If one of your things is calling it you should get some errors; once you fix them, if the function is still being called at this weird time, you know it's one of your things doing it.
oh wow it actually does. I just tried it in my game. it does call $$anonymous$$ain.
Just stick this code in any C# script and it will run without being called from anywhere. I think it has something to do with C# internally.
void $$anonymous$$ain()
{
Debug.Log("$$anonymous$$AIN!");
}
yeah.. I made a fresh project. It calls both "void $$anonymous$$ain()" and "IEnumerator $$anonymous$$ain()". I tried "void Start()" as a coroutine, and it seems to work, which also satisfies my use case, but it would have been nice not to have to spend 3 hours trying to figure out Unity automagically starting my coroutine..
Answer by Chris_Dlala · Jun 11, 2014 at 09:44 PM
Hi. I assume it is being run as an entry point of some kind for C#. This is the standard entry point for C# applications (see here). Completely avoid using it! I imagine you will get no consistent behaviour from it. I hope that helps =)