- Home /
Continuous music on selected scenes?
I found this code to play music through out multiple scenes, is there a way to play the music only on selected scenes and not all?
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
}
Answer by AlucardJay · Jul 21, 2014 at 08:35 AM
Okay, I get it know. Now I know what a singleton is too lol. I had trouble converting it to c# but i ultimately found the answer here: http://answers.unity3d.com/questions/31183/using-singleton-for-background-music.html. Thanks man.
Answer by Lucanio · Jul 21, 2014 at 08:35 AM
Do you mean you want the music to pause on certain scenes, but then pick back up at the same place in other scenes. Yes, yo can do that. To get the loaded level use Application.loadedLevel
or Application.loadedLevelName
. You can audio.Pause()
the music and because its gameobject has been set to DontDestroyOnLoad()
, it should resume when you call audio.Play()
in the specific scenes.
Additionally, you can get the specific position by calling audio.timeSamples
.
Thanks, it worked but now whenever I go back to the main menu or where the game object is, the game object duplicates itself. Is there a way to avoid this?
void Awake() { DontDestroyOnLoad(transform.gameObject); }
void Update () {
if(Application.loadedLevelName == "Stage")
{
audio.Pause ();
}
}
Your answer
Follow this Question
Related Questions
Play Audio Through Scenes 0 Answers
Multiple Cars not working 1 Answer
problems with continuous audio between scenes 1 Answer
Distribute terrain in zones 3 Answers