My question is how to play a background music on selected scenes only
I am making my first game and still focusing on the music part because it is the core of my game. I tried two different scripts the 2nd I think is a singleton that I got here but i dont know how to use it yet since I'm still exploring unity. I want to only play the background music on selected scenes because the game scene has its own music. Any help would be much appreciated :) Here is the first script which I tried but it is playing globally as it is said.
// Use this for initialization
void Start () {
}
// Play Global
private static BgSound instance = null;
public static BgSound Instance
{
get { return instance; }
}
void Awake()
{
if (instance != null && instance != this) {
Destroy (this.gameObject);
return;
} else {
instance = this;
}
DontDestroyOnLoad (this.gameObject);
}
and this one which works same as the first(?)
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(AudioListener))] [RequireComponent(typeof(AudioSource))] class Music : MonoBehaviour { private static Music instance = null; public static Music Instance { get { if (instance == null) { instance = (Music)FindObjectOfType(typeof(Music)); } return instance; } }
void Awake ()
{
if (Instance != this)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
Help! On/off button 1 Answer
Simple Movement Script Broken 1 Answer
How can I change my player control method to use a touch screen ?? Pong-clone 2D 0 Answers
Absolute beginner. What do I need to know? 1 Answer
{Newbie here} Anybody know a SIMPLE way to make my sprite/object shoot bullets? 2 Answers