Hi !
Anyone know how could I play a sound in my project with a delay of 10s ?
Thanks
!
Hi !
Anyone know how could I play a sound in my project with a delay of 10s ?
Thanks
!
Use a coroutine:
IEnumerator playSoundAfterTenSeconds()
{
yield return new WaitForSeconds(10);
audiosource.Play();
}
// then elsewhere when you want to invoke it:
void someOtherMethodInAMonoBehaviour()
{
StartCoroutine(playSoundAfterTenSeconds());
}
or even:
IEnumerator playSoundWithDelay(AudioClip clip, float delay)
{
yield return new WaitForSeconds(delay);
audiosource.PlayOneShot(clip);
}
// then elsewhere when you want to invoke it:
void someOtherMethodInAMonoBehaviour()
{
StartCoroutine(playSoundWithDelay(clipToPlay, delay));
}
Hope that helps.
The PlayDelayed() function on the AudioSource component should do the trick.
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.PlayDelayed( 10 );
Well then, whoβs a smarty bum!
![]()
The Unity Code Reference is your friend.
Always search there first. I use it dozens of times daily.
In factm I dont know how to make a script and everytime, in every scriptm I have problems, even I copy and paste from a script I have found on internet
β¦
Finaly I have created a delay on my sound with Audacity and itβs ok ^^
How would I make it so I can change the seconds in the inspector instead of adjusting it in the script every time?
public float delay;
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.PlayDelayed(delay);
Just in case, PlayDelayed does not respect Time.timeScale, so it may become a problem if you change time scale (e.g. in pause menu)
Hi
i tried the above code but it is not working. The code is correct. No errors are displayed on the console but the code is not getting implemented

This is coming in the inspector view but the code is not getting implemented in the file