- Home /
Continue playing sound effect on camera after LoadScene
I have a trigger with an OnTriggerEnter script. The script plays a sound effect (Camera.main.audio.PlayOneShot(audioFile);) and loads a new scene. When the new scene is loading the sound is not playing. I don't want to use the trigger as the sound source because the sound is not as clear as it does on the camera. How can I keep continue the sound on the main camera? I also don't want the main camera DontDestroyOnLoad because it is attached to the player.
Answer by LightSource · Mar 15, 2013 at 02:45 PM
That is quite a few questions. Consider posting multiple questions in the future if they get this long. Regardless, I think I can point you in the right direction.
First, it appears that System.Thread is supported. While your users are in the menu screen, you could have a thread routine run in the background and load all the GameObjects you will be using in the game. (Example below.)
To preserve all your game objects that you have loaded, you will need to call Object.DontDestroyOnLoad(/your object reference/) for each object. Alternatively, you could just add DontDestroyOnLoad(this) to the Awake() function of a MonoBehaviour script that is already attached to the loaded object. Note though that you will need to call Object.Destroy() if you decide to load a new scene and do not need those objects around anymore, otherwise they will persist until the application dies.
However, what about in the case if the user just blows through the menu screen and tries to jump into your game? I think there are two ways to deal with this.
The first way is to just block the user from starting the game by displaying a loading screen (e.g. using a GUITexture) with some sort of animation. Once the thread is finished, you can call Application.LoadLevel("Your Level"); to jump to the next scene. Of course, you still might have some down time while LoadLevel is invoked, so you could just make your loading screen object stick around with DontDestroyOnLoad() and then manually destroy it when the level is ready to go.
The second way is to note how far the thread has gotten, and then kill it. Put the note into some object that gets preserved into the next scene. When the new scene has loaded, continue loading where the previous thread left off. This is more complicated, however it might get your users playing faster if you prioritize the objects such that objects used sooner are loaded before objects that the player will not encounter for several seconds or more. You will need to use some sort of blocking mechanism if the user gets to a point where an object is needed, but it hasn't been loaded yet.
Threading example code: // ... in some MonoBehaviour objectpublic void Start(){ m_thread = new Thread(threadExecute);5. m_thread.Start();} private System.Thread m_thread;private float m_value = 0.0f;10. private void threadExecute(){ // ... normally you would perform object instantiation here. // Instead, lets do something intensive. This takes ~10 seconds 15. // per pass on 2nd gen iPod Touch with OS 3.1.2, Unity iPhone 1.5.1. // int loopCount = 0; while( true ) {20. Debug.Log("threadExecute process start for iteration " + loopCount.ToString()); for( int i = 0; i < 10000000; i++ ) m_value = Mathf.Sqrt((float)(i i i));25. Debug.Log("threadExecute process end for iteration " + loopCount.ToString()); loopCount++; 30. // ... ensure the thread will die if we stop playing in editor. // Otherwise, this thread will keep going until we start a // new debug session. :/ if(! Application.isPlaying ) break;35. } Debug.Log("threadExecute is exiting");}
Original Author:
Your answer
![](https://koobas.hobune.stream/wayback/20220613094453im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Losing reference to the camera after loading new scene 0 Answers
how do i trigger a camera and a sound 1 Answer
enter a trigger to access other camera? 1 Answer
Active camera on Start 1 Answer
Play sound only once 2 Answers