How do I make delays with coroutines?

Hi, I’ve been trying to use coroutines to create a delay but I can’t get it to work

public void PlayerSoundFXClip2(AudioClip audioClip, Transform spawnTransform, float volume)
    {
        //spawn in gameObject

        AudioSource audioSource = Instantiate(soundFXObject, spawnTransform.position, Quaternion.identity);

        //assign the audioClip

        audioSource.clip = audioClip;

        //assign volume

        audioSource.volume = volume;

        //play sound

        audioSource.Play();

        //get length of sound FX clip

        float clipLength = audioSource.clip.length;

        clipLength2 = clipLength;

        //destroy the clip after it is done playing

        Destroy(audioSource.gameObject, clipLength);

        Debug.Log("1");
        StartCoroutine(WaitTimer);
    }

    IEnumerator WaitTimer() {

        Debug.Log("2");

        yield return new WaitForSecondsRealtime(clipLength2);

        Debug.Log("3");

        PlayerMovement.instance.stepSound = false;
    }

}

Sorry if this seems like an easy fix but I’ve been banging my head against the wall for a while.

Im not clear what the wait is actually waiting on, but it looks like you’re making a cooldown, that is fine, what you have is good, but you dont seem to rely on stepsound anywhere else

Generally it is not advisable to use coroutines as timers, but in any case the code you have written seems to work fine, between logging 2 in the console and 3, there should be a delay equal to clipLength2 seconds. Where you intending for the delay to be somewhere else?

The stepsound is for another script, the wait is for whenever stepsound is triggered it gets set to true (in the other script) and after the wait it is set back to false (in this script) in order to stop the sound from spam playing.
The problem is whenever I use StartCoroutine(WaitTimer); I get the Assets/Scripts/SoundFXManager.cs(82,24): error CS1503: Argument 1: cannot convert from 'method group' to 'string' error .
Is there any other way to start a coroutine or any other way to make a timer?

Well, theres a bunch of opinions on starting coroutines you either start “WaitTimer” (eg as a string) or start WaitTimer()

Change it to StartCoroutine(WaitTimer());.

Yes, many, usually with the help of the update method by checking the time that has passed, or in more recent versions of Unity, with the help of Awaitable. The choice of implementation depends on your specific needs, including performance and desired features. You can find many different implementations on the internet.

I recently wrote a blog post showcasing three different timer implementations, each designed for distinct use cases, with the code available on GitHub. Check it out here: https://giannisakritidis.com/blog/Unity-Timers/ and the github code here: https://github.com/meredoth/Unity-Timers

I feel really dumb rn, the problem was that I put StartCoroutine(WaitTimer); instead of StartCoroutine(“WaitTimer”);

Thank you

I would advise if you go with the string option, to use the nameof(WaitTimer) route, as it will make refactoring easier if you later change the name of your method, by displaying an error in you IDE at compile time.

2 Likes

Ok, I’ll give it a try