• 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
0
Question by nastajus · Jul 04, 2014 at 03:34 AM · c#coroutinemonodevelopcoroutinescoroutine errors

Had difficulties implementing intro to Coroutines from unitypatterns.com. Help?

I can't figure out the cause of the problem. I've tried restarting Unity and rebooting my PC. I've tried passing string and method names. Switching 0 to null made no difference. Nothing ever changes. I'm always consistently getting either 1 Hello when yield comes before Debug.Log, and 2 Hellos when yield comes after. Any ideas??? I literally only put this online to ask this question here. I really think I need to have Coroutines work properly, I don't know what the source of the problem is.

Gist with syntax highlighting: https://gist.github.com/nastajus/e420713c2aa3be04b8cb

Github entire but simple project: https://github.com/nastajus/CoroutinesLearning

Unity Patterns tutorial: http://unitypatterns.com/introduction-to-coroutines/

The most interesting result was when I append i to the output it prints 5 times instead. Very weird and confusing based off the tutorial.

     void Start(){
         StartCoroutine("SayHello5Times");    //doesn't work right
     }

     IEnumerator SayHello5Times(){
         for (int i = 0; i < 5; i++){
  
             //instructions: comment out other block
  
             yield return 0;
             Debug.Log("Hello");            //prints once
             //Debug.Log("Hello " + i);    //prints 5 times
         }
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SirCrazyNugget · Jul 04, 2014 at 04:39 AM

The good news is your code works perfectly, the bad news is you're about to kick yourself. (I think anyway)

In the console you have an option to "Collapse" repeated items. I'm guessing you've got that on so only seeing repeated "Hello"'s only once though they have a counter on the right as to the number of times they've appeared. With the other lines with the appended i's as the comments are unique they're individually listed.

Comment
Add comment · Show 1 · 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 nastajus · Jul 04, 2014 at 04:47 PM 1
Share

PEB$$anonymous$$AC. sigh. I didn't see the little counter that accumulates all the identical messages. There's 92 hello's compounded in the image I posted on my github. I finally just noticed the little compound counter on the right side of the hello line. Thank you!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Coroutine Won't Stop Using IEnumerator 1 Answer

Running a coroutine without StartCoroutine 1 Answer

Coroutines not passing yield 1 Answer

Distribute terrain in zones 3 Answers

Using Time efficiently regardless of current frame rate. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges