How to directly put code inside of StartCoroutine?

I have a two functions:

void DoTheCoroutine () {

IEnumerator TheActualCoroutine() {
        // my code
        yield return null;

How can I put “My code” directly between the brackets of StartCoroutine() without declaring TheActualCoroutine()?

That’s not possible. There are two seperate concepts and the compiler doesn’t allow both in the same construct. First there are anonymous methods / lambda expressions. Second there are the so called generator / iterator methods (aka methods with “yield” that return either IEnumerator or IEnumerable).

The short answer is: The C# compiler simply doesn’t allow an anonymous method or lambda expression to be an iterator. If you want the long answer see the linked posts of Eric Lippert over here on SO. Eric Lippert was in the Microsoft development team that created the C# compiler.

@Bunny83 ,@ez06 you can do this:

public IEnumerator Co_Test(Action action)
        yield return action;

and use like this:

StartCoroutine(Co_Test(() =>
            Debug.Log("it work!!!!");

for convenience you can turn the coroutine to static and use it everywhere you want…
(i know you know that. said for others) :wink: