How do I add a delay in a script?

I tried two methods:

IEnumerator ResetLevel()
{
yield return new WaitForSeconds(5f);

MissileLauncher.missileSpeed += 1f; // Change the value based on how much you want the speed to increase

// You can use SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); if you want to reload the entire scene
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}

This one stopped at the yield return new line and the rest of the function wasn’t executed.

I tried the invoke line which never loaded the function:

void ResetLevelWithDelay()
{
Debug.Log(“Resetting with delay”);
Invoke(“ResetLevel”, 5.0f);
}

The script was attached to the enemy ship that was being destroyed. I figured it out by creating a LevelManager object and attaching a script to it to reset the level.

I use my CallAfterDelay class for delayed action.

See usage notes at bottom below gist code.