Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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#.

share|improve this question

closed as off-topic by Alexandre Vaillancourt, MAnd, Josh Petrie Jan 19 at 17:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Alexandre Vaillancourt, MAnd, Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

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

share|improve this answer
1  
Neat! I didn't know you could use coroutines as the Monobehaviour message functions like Start, etc. Very elegant. :) – DMGregory Jan 16 at 14:22
1  
Yea, but not with every message method such as OnDestroy. Don't remember the whole list :P – Hamza Hasan Jan 16 at 14:30
    
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. – DMGregory Jan 16 at 14:46
    
making sense with their names :). Well, please do let me know if you find anything official or confirm regarding this. – Hamza Hasan Jan 16 at 15:04

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.

share|improve this answer
    
would appreciate downvotes with a comment... – Daniel Netzer Jan 16 at 14:12
    
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. – DMGregory Jan 16 at 14:16
    
oh ok, I understand what you are talking about ill remove that Java explanation since its relevant to this question. – Daniel Netzer Jan 16 at 15:05
    
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. – DMGregory Jan 16 at 15:23
    
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. – Daniel Netzer Jan 16 at 15:42

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