x


how to play audio at a given time.

how to play audio at a given time,i mean i wanted to play audio after 1 min in the game. anyone please.

thank you

more ▼

asked Apr 05 '12 at 12:38 PM

Dee Va gravatar image

Dee Va
279 4 13 17

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

2 answers: sort voted first
var foo : AudioSource;

function Update () {

    if (!foo.isPlaying && Time.time >= 60) {
       foo.Play();
    }
}

Simple as that. ;)

Hope that helps, Klep

more ▼

answered Apr 05 '12 at 01:29 PM

Kleptomaniac gravatar image

Kleptomaniac
2.5k 6 11 21

It is not necessary (even buggy?) to ask to play it each frame once it is already playing, so this code is better:

if( !foo.isPlaying && Time.time >= 60 )
Apr 05 '12 at 01:53 PM Kryptos

Oh crap, sorry! Haha! What a stupid mistake! Literally facepalming right now ... Thanks for that pickup :D

Apr 05 '12 at 01:58 PM Kleptomaniac

Edited answer :D Hahaha :D

Apr 05 '12 at 01:58 PM Kleptomaniac
(comments are locked)
10|3000 characters needed characters left

You can use coroutines. Following code is in C#, but JS is quite similar.

IEnumerator PlaySoundAfterDelay( AudioSource audioSource, float delay )
{
    if( audioSource == null )
        yield break;
    yield return new WaitForSeconds( delay );
    audioSource.Play();
}

And to use it, you just need to start the coroutine:

public AudioSource myAudio;

void Start()
{
    StartCoroutine( PlaySoundAfterDelay( myAudio, 60.0f ) );
}
more ▼

answered Apr 05 '12 at 01:50 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

(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:

x1024
x570
x29

asked: Apr 05 '12 at 12:38 PM

Seen: 1004 times

Last Updated: Apr 05 '12 at 01:58 PM