-1
\$\begingroup\$
using UnityEngine;
using System.Collections;

public class WaitForSecondsExample : MonoBehaviour {

    void Start() {
        StartCoroutine(Example());
    }

    IEnumerator Example() {
        print(Time.time);
        yield return new WaitForSeconds(5);
        print(Time.time);
    }
}

I don't like using functions in my code. Instead of the example above, I'd prefer:

using UnityEngine;
using System.Collections;

public class WaitForSecondsExample : MonoBehaviour {

    void Start() {
        yield return new WaitForSeconds(5);
        print(Time.time);
    }

I can easily use yield In javascript but why can't I use yield in c# without using a function?

// Prints 0
print (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5);
// Prints 5.0
print (Time.time);

I have many problem with using functions in C#.

\$\endgroup\$
2
\$\begingroup\$

You can do it in C# too. You can use IEnumerator Start() instead of void Start().

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Neat! I didn't know you could use coroutines as the Monobehaviour message functions like Start, etc. Very elegant. :) \$\endgroup\$ – DMGregory Jan 16 '16 at 14:22
  • 1
    \$\begingroup\$ Yea, but not with every message method such as OnDestroy. Don't remember the whole list :P \$\endgroup\$ – Hamza Hasan Jan 16 '16 at 14:30
  • \$\begingroup\$ I'm trying to find an official list of which ones do/don't support coroutines, both to add to the answer and for my own notes. So far I've only found an unofficial thread reporting that Awake, Update, LateUpdate, FixedUpdate, OnGUI, OnEnable, OnDisable, OnDestroy, and OnCollisionStay cannot be used as coroutines, but I haven't verified all of these cases. \$\endgroup\$ – DMGregory Jan 16 '16 at 14:46
  • \$\begingroup\$ making sense with their names :). Well, please do let me know if you find anything official or confirm regarding this. \$\endgroup\$ – Hamza Hasan Jan 16 '16 at 15:04
-2
\$\begingroup\$

from Unity Documentation :: StartCoroutine

When using JavaScript it is not necessary to use StartCoroutine, the compiler will do this for you. When writing C# code you must call StartCoroutine.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ would appreciate downvotes with a comment... \$\endgroup\$ – Daniel Netzer Jan 16 '16 at 14:12
  • \$\begingroup\$ Patience, I'm still typing. ;) This answer is a bit misleading at present, bouncing between Java and Javascript, and making it sound like C# does not support threads - which of course it does. Coroutines are just another method to avoid holding up other work, even though they're still executed serially on the main thread. \$\endgroup\$ – DMGregory Jan 16 '16 at 14:16
  • \$\begingroup\$ oh ok, I understand what you are talking about ill remove that Java explanation since its relevant to this question. \$\endgroup\$ – Daniel Netzer Jan 16 '16 at 15:05
  • \$\begingroup\$ The amended answer just talks about the syntax for calling the coroutine, ie. whether to write StartCoroutine(WaitAndPrint(2.0)) vs WaitAndPrint(2.0) - this is still calling a separate coroutine, and doesn't address OP's question about whether one can simply wait within the body of Start, without chaining to a separate coroutine. \$\endgroup\$ – DMGregory Jan 16 '16 at 15:23
  • \$\begingroup\$ well the unity documentation and the quote pretty much clarify the concept of coroutines and the appropriate way to use them, and workaround as suggested by Hazma are purely coding trick which not always the best way to code since it might defect/damage the programs functionality. \$\endgroup\$ – Daniel Netzer Jan 16 '16 at 15:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.