- Home /
I am trying to play sound clip immidiately before loading new scene
I am trying to play sound clip immidiately before loading new scene.My sound clip does't fully get played before i switch to new scene.what should i do??
i can't use do not destroy on load.
Why can't you use dontDestroyOnLoad? Did you lose a bet?
Isn't the primary point of the music to play over the loading screen? Not just adding an extra delay to the game, then having the same silent, boring load screen as before?
Answer by bansalgirish8 · Apr 18, 2014 at 10:52 AM
Thanks everyone for your help. You helped me to track down the problem. Actually i was looking for rigidbody.constraints=RigidbodyConstraints.FreezeAll; I actually needed a replacement for Time.TimeScale=0. this is what i did:
using UnityEngine;
using System.Collections;
IEnumerator OnTriggerEnter( Collider _other )
{
if (_other.CompareTag ("w")) {
//Time.timeScale=0;
this.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
_other.rigidbody.constraints=RigidbodyConstraints.FreezeAll;
this.GetComponent< AudioSource>().priority=0;
audio.PlayOneShot(wall);
time = Time.timeSinceLevelLoad;
yield return new WaitForSeconds(0.3f);
Application.LoadLevel("GameOver");
}
}
Answer by GeorgesAbitbol · Apr 11, 2014 at 01:14 PM
Start playing the sound, and regularly check "yourAudioSource.isPlaying" to know when the sound has finished playing, then load the next scene.
EDIT: You could also check the AudioClip's length, start playing the sound, wait enough time, then load the next scene.
Answer by HarshadK · Apr 11, 2014 at 01:37 PM
You can load your level using a co-routine where in you put the co-routine to execute after the time duration of your audio clip.
Check this Psuedo-code in c#:
In OnTriggerEnter():
if(player dies){
play audio;
StartCoroutine("LoadNewLevel");
}
In your Co-routine:
IEnumerator LoadNewLevel() {
yield return new WaitForSeconds(TheTimeOfYourAudioClip);
Application.LoadLevel("YourLevel");
}
Let us know the outcome.
Thanks it helped to play whole sound clip but i can no longer stop the time scale. If i don't set time scale to 0 my player passes the enemies and if i use it the coroutine doesn't run this is what i did:
IEnumerator LoadNewLevel() { yield return new WaitForSeconds(2); Application.LoadLevel("GameOverNgui"); }
IEnumerator OnTriggerEnter( Collider _other ) { if (_other.CompareTag ("Skull")) {
this.GetComponent< AudioSource>().priority=0; audio.PlayOneShot(wall);
Time.timeScale=0; StartCoroutine("LoadNewLevel"); } }
I think co-routine approach towards this problem is not 100% correct.
Reason: If you have more than one buttons e.g b1 and b2, and you want to load scene by pressing b1. After pressing b1 your co-routine will start and at the same time you press b2 then what happen?
For this problem, I will recommend make a singleton class and attach general audio source and audio listener, and call DontDestroyOnLoad(audiolistener) method in this singleton class awake method and reside this singleton class where you need audio listener and audioSource.
if(player dies){ play audio; StartCoroutine("LoadNewLevel"); }
IEnumerator LoadNewLevel() { while(audiosource.isPlaying) { yield return null; }
Application.LoadLevel("YourLevel"); }
Your answer
![](https://koobas.hobune.stream/wayback/20220613142451im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
problems with continuous audio between scenes 1 Answer
5.1 Channels is backwards in unity (non 3D) 0 Answers
Application.LoadLevel(); not working 2 Answers
Downloading Audio Durin RunTime.. 1 Answer
Audio sounds on gameobject 0 Answers