Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Sound not playing when dieing

Discussion in 'Scripting' started by Zero101, May 30, 2015.

  1. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    So I want the game to play a sound(did that part to make the sound play) when the player dies(did the part where you can die) but I did found out how to delay that but the delay is being activated once so basicly look at the code and you are going to understand what i mean
    Code (CSharp):
    1. IEnumerator MyCoroutine(){
    2.         if (health <= 0) {
    3.             DSound ();
    4.        
    5.             yield return 75;    //Wait 75 frames aka 2 and a half seconds
    6.        
    7.             Die ();
    8.                      
    9.                         Now here something that checks again the  "if" sentence
    10.         }
    11.     }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    Did not help me.... Now here is the new code:
    Code (CSharp):
    1. IEnumerator MyCoroutine(){
    2.        
    3.         if (health <= 0) {
    4.             DSound();
    5.             for (int i=0; i<75; i++){
    6.                 ADSound();              //Now I should die 75 times.......... So did not help me at all.....people please just help me! :(
    7.                 yield return null;
    8.             }
    9.         }
    10.     }
    11.  
    12.  
    13.  
    14.     void ADSound(){
    15.         Destroy(gameObject);
    16.     }
    17.     void DSound(){
    18.         audio.PlayOneShot(Explosion, 0.7F);
    19.     }
     
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Put ADSound() after the for loop