Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Audio Player Death Audio Not Playing in Coroutine.

Discussion in 'Audio & Video' started by AtlasUnity, Apr 28, 2024.

  1. AtlasUnity

    AtlasUnity

    Joined:
    Sep 23, 2021
    Posts:
    5
    I have a Player Death Script which would start a coroutine if the Player's health is <= 0.

    UnityCode2.png UnityCode.png

    Most of the Coroutine plays out the way it should. The only problem is the AudioSource deathScream, it doesn't immediately play when the coroutine starts and sometimes it doesn't play at all. Is there something I'm missing with my code? I look forward to an answer.
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,306
    When your health drops to zero or below, you will fire the Death coroutine every single frame, and if you play an audiosource every single frame it keeps trying to play the clip from the beginning, but never gets a chance to play through it

    You need to only be calling that Death coroutine once, so use a flag to determine whether they're already dying or not.
     
  3. AtlasUnity

    AtlasUnity

    Joined:
    Sep 23, 2021
    Posts:
    5
    Should I use a Bool to check if the play has died? Then if it’s true then Start Coroutine?
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,306
    Code (CSharp):
    1. if (currentHealth <= 0 && bIsAlive)
    2. {
    3.   bIsAlive = false;
    4.   ....PlayerHasDied();
    5. }
     
  5. AtlasUnity

    AtlasUnity

    Joined:
    Sep 23, 2021
    Posts:
    5
    Hey brotha, just to let ya know I finally got it to work. Apparently the real problem was that my health variable is static and is being updated, so I just made it a normal float instead. Thx for the help tho.
     
    SeventhString likes this.