- Home /
 
 
               Question by 
               Babilinski · Jan 09, 2012 at 04:16 AM · 
                c#audioif-statementsifaudioplay  
              
 
              play an audio clip in an if statement
I have a dialogue that gets turned on and off with if statements. I wanted to know how you could make an audio file play only once when in a if block
     if(StartingQuest == true)
         
       audio.clip = intro;
     audio.Play();
     
     if(QuestOne == true)
     audio.clip = firstQuest;
     audio.Play();
     
     if(QuestOne_done == true){
         audio.clip = Getting_sword;
          audio.Play();
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by sriram90 · Jan 09, 2012 at 04:28 AM
Hi Babilinski ,
Here is your answer. its very simple. just make function for everything and call it.
 public AudioClip gameClip;
     
 void Update()
 {
     if(StartingQuest)
     {
            playMusic();
     }
 }
     
 void playMusic()
 {
     audio.clip = gameClip;
     audio.Play();
     audio.volume = 1.0f; // optional
     audio.loop = false; // for audio looping
 }
 
              Answer by adrenak · Jan 09, 2012 at 04:31 AM
You have to change the value of the boolean variables that you have after intro clip has played. Initialise all boolean variables except StartingQuest to false and then make use of yield WaitForeSeconds:
 if(StartingQuest == true){
 
       audio.clip = intro;
       audio.Play();
       yield WaitForSeconds (audio.clip.length);
       QuestOne = true;  
 }    
 
 if(QuestOne == true){  //now questone can play
     audio.clip = firstQuest;
     audio.Play();
     yield WaitForSeconds (audio.clip.length);
 
     //now question one has completed
     QuestOne_done = true;
 }
 
 if(QuestOne_done == true){  //play what is to be played when QuestOne is done
      audio.clip = Getting_sword;
      audio.Play();    
      //then again 
      yield WaitForSeconds (audio.clip.length);
 }  
 
              Your answer