Search Unity

Audio AudioSource playing after game ends

Discussion in 'Audio & Video' started by bgwissam, Jan 24, 2019.

  1. bgwissam

    bgwissam

    Joined:
    Jan 15, 2019
    Posts:
    3
    Hi All,
    I'm new to unity and currently struggling with some Audio issue. I've set a countdown time in my game which when reaches 10 seconds a "Tic-Tac" sound will start playing.

    Code (CSharp):
    1. private IEnumerator GameLoop()
    2.     {
    3.         yield return StartCoroutine(StartGame());
    4.         yield return StartCoroutine(PlayGame());
    5.         yield return StartCoroutine(EndGame());
    6.  
    7.         if (counter == 1 && elapsedTime >-1)
    8.         {
    9.             Time.timeScale = 0;
    10.         }
    11.         else if(elapsedTime < 0)
    12.         {
    13.             Time.timeScale = 0;
    14.             restartButton.SetActive(true);
    15.         }
    16.  
    17.         else
    18.         {
    19.             StartCoroutine(GameLoop());
    20.         }
    21.     }
    22.  
    23. private IEnumerator PlayGame()
    24.     {
    25.         EnableControl();
    26.         messageText.text = string.Empty;
    27.         while (counter < 1 )
    28.         {
    29.             if (elapsedTime < 11)
    30.             {
    31.                 StartCoroutine(TenSecondSound(1.0f));
    32.              
    33.             }
    34.  
    35.             if (elapsedTime < 0)
    36.             {
    37.                 break;
    38.             }
    39.             yield return null;
    40.         }
    41.     }
    42.  
    43. IEnumerator TenSecondSound(float t)
    44.     {
    45.  
    46.        
    47.         otherSounds.clip = ticTac;
    48.         otherSounds.Play();
    49.  
    50.         Debug.Log("Playing " + elapsedTime);
    51.  
    52.         yield return new WaitForSeconds(t);
    53.        
    54.     }
    The TenSecondSound executes the I can see the log message displayed, however the sound only plays when the loop ends.
    I tried using otherSounds.PlayOneShot(tic_tok);
    but the sound plays once and it's all cramped up I don't know why