🤔

CS1624#

The body of 'Foo.Bar()' cannot be an iterator block because 'type' is not an iterator interface type

The yield keyword can only be used in an iterator context. This requires it to be returning specific interface types. Typically with Unity this keyword is used in the context of a Coroutine.

Resolution#

Convert your method to a coroutine if waiting is required, and start it appropriately.

Example#

public void Start() => Function();
public void Function()
{
// ...
yield return new WaitForSeconds(1);
// ...
}

becomes:

public void Start() => StartCoroutine(Function());
public IEnumerator Function()
{
// ...
yield return new WaitForSeconds(1);
// ...
}
information

Coroutines must also be started correctly, and not started continuously as they are automatically continued until they finish or are cancelled. See StartCoroutine and StopCoroutine for more information.