Help with WaitForSeceonds please

I have the code

 if (speed > 0)
        {
            waitForSeceonds(speed);
            Vector3 position = new Vector3(xPos, yPos, 0);
            Instantiate(enemy, position, Quaternion.identity);
        }
    }
    IEnumerator waitForSeceonds(int waitTime)
    {
        yield return new WaitForSecondsRealtime(waitTime);
    }

but it won’t work. I tried a ton of different things but it won’t work. Any help is very appreciated.

WaitForSeconds works only inside a Coroutine, and a Coroutine must be started via StartCoroutine.

The problem for you is that StartCoroutine will return immediately, so you must move the entire code that follows waitforseconds into the coroutine, something like below.

if (speed > 0)
        {
            StartCoroutine(waitForSeceonds);
        }
    }

    IEnumerator waitForSeceonds(int waitTime)
    {
        yield return new WaitForSecondsRealtime(waitTime);
        Vector3 position = new Vector3(xPos, yPos, 0);
         Instantiate(enemy, position, Quaternion.identity);
    }

However, that code won’t work correctly, as the coroutine must not be a local function, and the variables won’t work. Next time, you’d probably receive better hints if you publich the entire code.