Countdown Timer Script with Audio Queues

Hi, I’ve built a game with a timer, and there are sounds that play at specific points in time. Using the code below, only my ending sounds work (stopping the music & playing final game over sound) but none of the sounds along the way are played. Can anyone say what I’m doing wrong?

Thanks for any advice!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class UICount : MonoBehaviour

{

    [SerializeField] float startTime;
    [SerializeField] TMP_Text timerText;
    float currentTime;
    bool timerStarted = false;
    public AudioSource EndGameAudio;
    public AudioSource HelpMe;
    public AudioSource Mommy;
    public AudioSource Music;
    public AudioSource Hurry;


    public bool playing;
    private float Timer;

    // Start is called before the first frame update
    void Start()
    {
        currentTime = startTime;
        timerStarted = true;

    }

    // Update is called once per frame
    void Update()
    {
        if (timerStarted)
        {
            currentTime -= Time.deltaTime;

            if (currentTime == 150f)
            {
                HelpMe.Play();
            }

            if (currentTime == 115f)
            {
                Mommy.Play();
            }

            if (currentTime == 15)
            {
               Hurry.Play();
            }

            if (currentTime <= 0)
            {
                timerStarted = false;
                currentTime = 0;
                Music.Stop();
                EndGameAudio.Play();

            }

            timerText.text = currentTime.ToString("F1");
        }

    }
}

Never compare floating point values for equality. Here’s why:

https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

Instead, check it goes below and has not been started, then start it.

This makes sense. Can I just change them all to equal or less like the below or will that make them repeat? Maybe I should just try it haha. Thank you!

if (currentTime <= 150f)
            {
                HelpMe.Play();
            }

I LIKE THAT ATTITUDE!!! That my friend is how you figure stuff out!!!

Actually though, I don’t think you can quite get away with it in this case: it will ceaselessly play again and again.

But here is one thing you CAN do, assuming you can reload this scene before you need it a second time:

            if (currentTime < 115f)
            {
                // test the reference so we know we didn't clear it already
                if (Mommy)
                {
                     Mommy.Play();
                     Mommy = null;       // clear the reference so it only plays once
                }
            }

The AudioSource Component itself will stay (of course), but your reference to it in this script will be nulled, which will prevent you from constantly Play()-ing it.

Then when you reload the scene (or if it’s a prefab, reinstantiate it), all those values will be repopulated.