- Home /
[SOLVED] Issue with music and Don't Destroy on load
I have the following code:
public class MainMenuAudio : MonoBehaviour {
private static MainMenuAudio instance;
void Awake() {
if (MainMenuAudio.instance == null)
{
MainMenuAudio.instance = this;
GameObject.DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
void Start()
{
}
public void turnMusicOff()
{
if (instance != null)
{
this.gameObject.audio.Stop();
Destroy(this.gameObject);
MainMenuAudio.instance = null;
}
}
void OnApplicationQuit() {
MainMenuAudio.instance = null;
}
}
What it is supposed to do is to the keep the music going through the various menu scenes until you choose to start a game. Also it allows the game to start if the music is set to off in the options. The problem I am having is that when you exit the scene to another menu the music is not continuing. Yet when you return the music is playing.
Please help.
Note: The object is currently parented under the camera as has been suggested.
When it is unparented (at the top of the hierarchy). DontDestoryOnLoad works as expected, however it prevents the two game levels from loading, yet switches between menu scenes without issue. Additionaly it does not respond to the turnmusicoff function above when the music option is set to off.
Update 6/9/13: after 2 not so hopeful answers not even written in the correct language I came upon this. http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html
So I first put the audio gameobject into the top hierarchy and changed the code to this so now it works.
public class MainMenuAudio : MonoBehaviour
{
private static MainMenuAudio instance;
void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject);
return;
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
void Start()
{
if (!instance.audio.isPlaying)
audio.Play();
}
public void turnMusicOff()
{
if (instance != null)
{
//if (instance.audio.isPlaying)
// instance.audio.Stop();
Destroy(this.gameObject);
instance = null;
}
}
void OnApplicationQuit()
{
instance = null;
}
}
Answer by Dee Va · Jun 09, 2013 at 07:45 AM
Attach to a game object..
public var SoundClip : AudioClip;
private var SoundSource : AudioSource;
function Awake()
{
DontDestroyOnLoad(gameObject);
SoundSource = gameObject.AddComponent(AudioSource);
//SoundSource.playOnAwake = false;
//SoundSource.rolloffMode = AudioRolloffMode.Logarithmic;
//SoundSource.loop = true;
}
function Start()
{
SoundSource.clip = SoundClip;
SoundSource.Play();
}
The code above in addition to not being written in C# as $$anonymous$$e was. Didn't resolve the issue, I don;t think it even attempted to resolve it and it would reintroduce an issue with duplication of the audio.
Answer by DricoJD · Jun 09, 2013 at 08:16 AM
I have no clue, why you would make different scenes for a Main Menu, when you should just make it in one scene; but there is no argument for what or not should you choose.
Sound Sources run off Logarithms. I only use Java, so the only script I can give you is this.
Create a Empty Game object and attach this too it.
var SoundClip : AudioClip;
var SoundSource : AudioSource;
function Awake()
{
SoundSource = gameObject.AddComponent(AudioSource);
DontDestroyOnLoad(gameObject);
//This is played every time the script is called.
}
function Start()
{
SoundSource.Play();
SoundSource.clip = SoundClip;
//This is played once at the start of the script.
}
Did you find this helpful? Please thumb up if so :)
The reason among many is
UnityGUI has gone crazy on me and I was forced to rely on NGUI.
There are menu options that require different scenes, and loading them all into one would increase load time.
NGUI plays nicest when using a single camera and atlas for the gui and a seperate camera for the game scene.
If you only use java, why did you bother giving an answer especially since it is a repeat of the one above?
Because you do not have to stick with one language in a project. so you can make a new js script and paste this in.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103801im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity - Play AudioSource without delay 1 Answer
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Audio: how do I immediately play a new audioclip without delay(code included)? 2 Answers