- Home /
Menu music stops playing after returning from game level
Hello everyone! I have a problem with my game music and I can't figure out what's the problem. I have "MenuMusic" gameobject in main menu scene with DontDestroyOnLoad script attached to it. when I go to the character selection menu or achievements screen, everything works fine - music continues to play. My game level has a script that kills "MenuMusic" object. When I exit back to main menu, the MenuMusic starts playing, but when I go to other menu scenes, the music stops and I can't figure out why. Long story short - After I have played the game level, menu music is playing only in Main menu scene.
This is the code attached to main menu gameobject. However, after I comment out the Destroy(this.gameObject); the music continues to play in other menu scenes, but instantiates after returning back to main menu. What could be wrong?
using UnityEngine;
using System.Collections;
public class DontDestroy : MonoBehaviour
{
static bool AudioBegin = false;
GameObject otherSound;
// Use this for initialization
void Awake()
{
otherSound = GameObject.FindGameObjectWithTag("MenuMusic");
if (otherSound == this.gameObject)
{
if (!AudioBegin)
{
DontDestroyOnLoad(this.gameObject);
AudioBegin = true;
}
}
else
{
Destroy(this.gameObject);
}
}
}
Okay, I just managed to find the solution myself. Just in case someone else stumbles upon such problem. Adding these lines solved the problem for me.
void Start () { DontDestroyOnLoad(this.gameObject); }
Your answer
![](https://koobas.hobune.stream/wayback/20220613191338im_/https://answers.unity.com/themes/thub/images/avi.jpg)