• 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 Tsanas · Feb 20, 2012 at 03:20 PM · coroutineyieldienumeratorreturnienumerable

Corutine not behaving like it should?

Hello there..

Alright, so im new to the whole yield keyword and corutines in unity. So im pretty confused as it is, and not having it behave like it 'should', doesnt make it easier :P

Anyway, here's what i want to do:

This is taken directly from here.


 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
     IEnumerator Do() {
         print("Do now");
         yield return new WaitForSeconds(2);
         print("Do 2 seconds later");
     }
     void Example() {
         Do();
         print("This is printed immediately");
     }
 }



And this is my code:


 private void SetView()
 {
     ActivateFirstPersonView();
     Debug.Log("This is immediately after");
 }

 public IEnumerator ActivateFirstPersonView()
 {
     Debug.Log("Starting");
     yield return new WaitForSeconds(2);
     Debug.Log("Finished");
 }



The problem is, 'ActivateFirstPersonView' isnt being called at all.

If i use,

 StartCoroutine(ActivateFirstPersonView());

instead. Then it behaves like it should, I just dont get it?

Is it a documentation error? I hope not, because i would like to call the method without starting a coroutine, for simplicity's sake.

Comment
Add comment · Show 6
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 Jessy · Feb 20, 2012 at 04:07 PM 0
Share

"i would like to call the method without starting a coroutine, for simplicity's sake." That doesn't make any sense. Please elaborate

avatar image runonthespot · Feb 20, 2012 at 05:58 PM 0
Share

If you create a blank project, does the sample code not work?

avatar image Tsanas · Feb 20, 2012 at 06:33 PM 0
Share

@Jessy I dont want to be required to use StartCoroutine whenever i want to call 'ActivateFirstPersonView' - Somewhere along the development, I, or someone else working on the project, might forget it, and end up with a function that doesnt get called.

And possibly, we could end up spending hours figuring it out. And according to the documentation, it shouldnt be required.

@$$anonymous$$ike I'll try that asap.

avatar image Jessy · Feb 20, 2012 at 06:48 PM 2
Share

The documentation is bugged; try it. That's what happens when the script reference is only auto-converting the original UnityScript. It explicitly states here that you need to use StartCoroutine (unless you plan to move through it manually, but that won't run as efficiently, and will require more code than the function call).

http://answers.unity3d.com/questions/14495/confused-about-coroutines.html

avatar image PrimeDerektive · Feb 20, 2012 at 08:41 PM 0
Share

Yeah, you need to use StartCoroutine to start coroutines in C#. That looks like a docs bug.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Tsanas · Feb 21, 2012 at 03:42 AM

Thanks for your comments guys.

This is what i might end up with:


 Class 1:

 private void SetView()
 {
     StartFirstPersonView();
     Debug.Log("This is immediately after");
 }

 Class 2:

 public void StartFirstPersonView()
 {
     StartCoroutine(ActivateFirstPersonView());
 }
 
 private IEnumerator ActivateFirstPersonView()
 {
     Debug.Log("Starting");
     yield return new WaitForSeconds(2);
     Debug.Log("Finished");
 }



It might be a bit exessive, but i want to keep all the coroutine responsibility inside class 2.

If you guys have a more clever way of doing this, let me know.

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 Bunny83 · Feb 21, 2012 at 05:31 AM 1
Share

It's fine this way. Also keep in mind that coroutines always run on a $$anonymous$$onoBehaviour instance, so it's always better to use Startcoroutine of the script to which the coroutine belongs to.

If the script instance gets destroyed all coroutines that run on this script are terminated immediately.

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

10 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

Related Questions

What am I doing wrong with IEnumerator? 1 Answer

How do I return a value from a coroutine? 8 Answers

[ Coroutine: Move Next ] CPU usage 0 Answers

Pause the coroutine inside a loop 1 Answer

Coroutoutine doesnt work properly. 3 Answers


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