- Home /
Is there a secret Main() coroutine?
Hi there
I've been hitting my head against the wall for a few hours trying to work out why some timing in a coroutine wasn't working, and it eventually looked like it was getting called twice. By adding this code:
last_time = Time.time;
yield return new WaitForSeconds(2);
Debug.Log("Time passed " + 1000 * (Time.time - last_time));
I had succesive print outs of "Time passed 2000", then instantly "Time passed 0", then another 2s pause etc.
Having search the code base for all instances of StartCoroutine and of this function, nothing was inadvertently calling it. I added this a message at the top of the coroutine to print out some text:
public IEnumerator Main()
{
Debug.Log("Running bug main");
And lo and behold, its printed out twice. I comment out my call to StartCoroutine and it only gets printed once instead. Again, there are no calls to StartCoroutine that run this function (there's only 2 others in total), no other calls to it, and just to be sure, if I rename the function I don't hit any compile errors.
The really weird thing is that if I rename the function the problem goes away:
public IEnumerator BugMain()
{
Debug.Log("Running bug main");
And everything is fine.
Have I missed something here? I can't find any references to a special use of the word 'Main' in unity, and yet it's clearly doing something special here.
cheers
-Chris
I would have avoided "$$anonymous$$ain" out of habit, but I can confirm the effect. If I include a function named "$$anonymous$$ain", it will be called between Awake and Start. I don't see this documented anywhere.
Answer by tanoshimi · Sep 12, 2014 at 04:19 PM
Yes, there is a "secret" Main() function, which is discussed in this forum post: http://forum.unity3d.com/threads/trick-start-and-update-without-start-and-update-function.252577/
Wow - well good to know. Guess I'll be steering well away from that then - would have been lovely if somewhere the docs mentioned it! Cheers for the answer :)
Wonder if this is related http://answers.unity3d.com/questions/731632/when-js-scripts-compiles-into-a-dll-the-start-upda.html