# Delay in the middle of a script in Unity without calling a separate coroutine [closed]

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#.

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

• Neat! I didn't know you could use coroutines as the Monobehaviour message functions like Start, etc. Very elegant. :) – DMGregory Jan 16 '16 at 14:22
• Yea, but not with every message method such as OnDestroy. Don't remember the whole list :P – Hamza Hasan Jan 16 '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 '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 '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.

• would appreciate downvotes with a comment... – Daniel Netzer Jan 16 '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 '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 '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 '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 '16 at 15:42