x


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();
more ▼

asked Jan 09 '12 at 04:16 AM

Babilinski gravatar image

Babilinski
119 40 42 43

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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
}
more ▼

answered Jan 09 '12 at 04:28 AM

sriram90 gravatar image

sriram90
475 33 37 47

(comments are locked)
10|3000 characters needed characters left

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);
}  
more ▼

answered Jan 09 '12 at 04:31 AM

vatsalAtFEI gravatar image

vatsalAtFEI
734 13 19 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4374
x1060
x156
x84
x29

asked: Jan 09 '12 at 04:16 AM

Seen: 1392 times

Last Updated: Jan 09 '12 at 04:31 AM