Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by wibble82 · Sep 12, 2014 at 02:42 PM · coroutineyieldmain

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Sep 12, 2014 at 03:55 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
5

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/

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wibble82 · Sep 12, 2014 at 04:34 PM 0
Share

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

avatar image vexe · Sep 12, 2014 at 04:58 PM 0
Share

Wonder if this is related http://answers.unity3d.com/questions/731632/when-js-scripts-compiles-into-a-dll-the-start-upda.html

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need to call yield TWICE ??? (ANSWERED) 3 Answers

Respawn after delay 3 Answers

Yield WaitForSeconds doesn't work, give syntax error 1 Answer

Coroutine a function within a loop? 1 Answer

YieldInstructions from unmanaged land e.g. WaitForSeconds. Possible? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges