Question by
Jojoba007 · Aug 06, 2017 at 08:28 AM ·
scripting problemmusicsingleton
Music Manager singleton change song
I have an Music Manager in my main menu scene that is a singleton. The reason for the singleton is that I wan't to play some songs through different scenes. So far so good...
But from the Music Manager I would also like to load different songs on specific scenes. So if a specific scene gets loaded the song should change.
How would I do this from within the Music Manager which is a singleton?
I tried it with checking which scene is loaded at that present moment but I can't seem to figure out how to properly do this. Any suggestions are welcome.
This is the code I tried for the Music manager:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class MusicManager : MonoBehaviour {
public static MusicManager instance;
// Vars for audio
private AudioSource audioSource;
public AudioClip MainMenuSongAU;
public AudioClip GreenHillsSongAU;
public AudioClip CaveSongAU;
private bool SongLoaded;
// Vars for checking which scene has been loaded at present moment
private string lastScene;
private string currentScene;
void Awake () {
MakeSingleton ();
audioSource = GetComponent<AudioSource> ();
//Getting the scenename
lastScene = SceneManager.GetActiveScene().name;
// Setting the music state standard to 1 so music starts playing on game load
if(!PlayerPrefs.HasKey ("Game Initialized")) {
MusicState.SetMusicState (1);
PlayerPrefs.SetInt(" Game Initialized", 123);
}
}
void Update() {
// Checking which scene is loaded (for handling music)
currentScene = SceneManager.GetActiveScene().name;
if(currentScene != lastScene) {
lastScene = currentScene;
ChangeSong();
}
}
// Changing the song when loading a specific level
void ChangeSong() {
if (lastScene == "GameStartInfo") {
audioSource.PlayOneShot(GreenHillsSongAU, 0.4f);
Debug.Log ("Var lastScene is now: " + lastScene);
}
}
void MakeSingleton () {
if (instance != null) {
Destroy (gameObject);
} else {
instance = this;
DontDestroyOnLoad (gameObject);
}
}
public void PlayMusic (bool play) {
if (play) {
if (!audioSource.isPlaying) {
//audioSource.Play();
audioSource.PlayOneShot(MainMenuSongAU, 0.4f);
audioSource.loop = true;
}
} else {
if (audioSource.isPlaying) {
}
audioSource.Stop ();
}
}
}
And this is the code for the Menu Controller and a music On/Off button that activates a function inside the Music Manager (MusicState and PlayMusic():
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MainMenuController : MonoBehaviour {
[SerializeField]
private Button musicButton;
[SerializeField]
private Sprite[] musicIcons;
private Text highScoreMainText;
void Awake ()
{
highScoreMainText = GameObject.Find("HighScoreTextAmountMain").GetComponent<Text> ();
SetHighMain ();
}
void Start () {
CheckToPlayTheMusic ();
}
public void PlayGame ()
{
SceneManager.LoadScene ("GameStartInfo");
}
public void SetHighMain ()
{
int score = PlayerPrefs.GetInt("HighScore");
highScoreMainText.text = "" + score;
}
void CheckToPlayTheMusic () {
if (MusicState.GetMusicState () == 1) {
MusicManager.instance.PlayMusic (true);
musicButton.image.sprite = musicIcons [0];
} else {
MusicManager.instance.PlayMusic (false);
musicButton.image.sprite = musicIcons [1];
}
}
public void MusicButton () {
if (MusicState.GetMusicState () == 0) {
MusicState.SetMusicState (1);
MusicManager.instance.PlayMusic (true);
musicButton.image.sprite = musicIcons [0];
} else if (MusicState.GetMusicState () == 1) {
MusicState.SetMusicState (0);
MusicManager.instance.PlayMusic (false);
musicButton.image.sprite = musicIcons [1];
}
}
}
Comment