Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity - Add Background Music on Game

I want to add some music in my game that can play in every scene, and if scenes change music doesn't start again and can be turn off on setting menu

can someone help me to figure it out?

like image 989
Raka Arya Pratama Avatar asked Oct 15 '25 20:10

Raka Arya Pratama


1 Answers

What have you tried so far? Show your code. There is a way to achieve what you want, that is by using DontDestroyOnLoad function. Create a gameObject, add AudioSource to it and then add the below script to that gameObject:

public class AudioPlayerManager: MonoBehaviour
{
      private static AudioPlayerManager instance = null;
      private AudioSource audio;

      private void Awake()
      {
          if (instance == null)
          { 
               instance = this;
               DontDestroyOnLoad(gameObject);
               return;
          }
          if (instance == this) return; 
          Destroy(gameObject);
      }

      void Start()
      {
         audio = GetComponent<AudioSource>();
         audio.Play();
      }
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!