StartCoroutine gets the arglist type wrong

In the following code snippet, the line containing the StartCoroutine results in the error “No appropriate version of ‘UnityEngine.MonoBehaviour.StartCoroutine’ for the argument list ‘(void)’ was found.” As you can see, the argument list is in no way void.

Switching to string call syntax (instance.StartCoroutine("FadeInAgain", source)) gives no compile errors, but at runtime, the error message “Coroutine ‘FadeInAgain’ couldn’t be started! UnityEngine.MonoBehaviour:StartCoroutine(String, Object)”.

This behaviour is all the more puzzling as the exact same call sequence works perfectly in other contexts, even in the same source file. (The code below is the smallest version of the file which still gives this error.) I should also point out that omitting the “static” declaration for the coroutine makes no difference.

Any ideas?

#pragma strict
#pragma downcast

static var instance : AudioManager;

function Awake() {
	instance = FindObjectOfType(AudioManager) as AudioManager;

static function RestoreAffectedSounds(source : AudioSource) {

static function FadeInAgain (source : AudioSource) {

Actually, the argument IS void in this situation.

This is because the function FadeInAgain implicitly returns void because there is no return statement. The complier sees no return statement and then adds return void; to the function implicitly. StartCoroutine() is looking for a function that returns an IEnumerator.

A function returns an IEnumerator implicitly if and only if it contains the statement


the compiler reads this as

yield return null;

Your function now returns an IEnumerator, and StartCoroutine() will accept it as an argument. However, it cannot return any useable data other than an IEnumerator.

It’s hard to see in JavaScript because it tries to handle everything behind the scenes. C# forces you to do everything explicitly, so some people prefer using it because it give you more information on what’s going through the compiler’s mind.

For more information on this subject, read the coroutine documentation and select the language C#.