Using Grid-like singletons for continuous Audio
In my efforts to swap from singletons to @Fattie 's proposed 'Grid system' - I can't seem to get my audio continuing between scenes.
I thought that the instances are created in preload and everything the persists but I'm not observing that behaviour. Music just cuts off between scenes and I can't reference any of the Grid.controller.functions unless I create instances of them. I'm new to all of this..c# and unity so likely being a monkey somewhere! Thanks for any guidance.
My Grid.cs
using UnityEngine;
static class Grid
{
public static GameController game;
public static AudioController audio;
public static CameraController camera;
public static BackgroundsController backgrounds;
static Grid()
{
GameObject g;
g = safeFind("manager");
game = (GameController)SafeComponent( g, "GameController" );
audio = (AudioController)SafeComponent( g, "AudioController" );
camera = (CameraController)SafeComponent( g, "CameraController" );
backgrounds = (BackgroundsController)SafeComponent( g, "BackgroundsController" );
}
private static GameObject safeFind(string s)
{
GameObject g = GameObject.Find(s);
if ( g == null ) Error("The " +s+ " game object is not in this scene.");
return g;
}
private static Component SafeComponent(GameObject g, string s)
{
Component c = g.GetComponent(s);
if ( c == null ) Error("The " +s+ " component is not there.");
return c;
}
private static void Error(string error)
{
for (int i=10;i>0;--i) Debug.LogError(" >>>>>>>>>>>> Cannot proceed... " +error);
for (int i=10;i>0;--i) Debug.LogError(" !!! Is it possible you just forgot to launch from scene zero, the preload scene.");
Debug.Break();
}
}
And my Preload...
using UnityEngine;
using UnityEngine.SceneManagement;
public class PreloadController : MonoBehaviour {
public static GameObject manager;
void Awake() {
DontDestroyOnLoad (manager);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Application.GetStreamProgressForLevel("GameTitle") ==1){
SceneManager.LoadScene("GameTitle");
}
}
}
And my AudioController
public class AudioController : MonoBehaviour {
public static AudioController control; // TBH, not sure why I need this but complains about it being destroyed otherwise
// CURRENTLY PLAYING MUSIC
public AudioSource musicAudioSource;
// MUSIC
public AudioClip ocean;
private bool isMusicMute;
private string musicCurrentlyPlaying;
void Awake() {
control = this;
}
public void PlayMusicByStage(int stage) {
switch (stage) {
case 1:
PlayMusicByName ("ocean");
break;
}
}
public void PlayMusicByName(string name) {
if (control.musicCurrentlyPlaying != name) {
switch (name) {
case "ocean":
control.musicAudioSource.clip = ocean;
break;
}
control.musicCurrentlyPlaying = name;
}
if (!control.musicAudioSource.isPlaying) {
control.musicAudioSource.Play ();
}
if (!Grid.game.isMusic) {
Mute ();
}
}
public void StopAllMusic () {
control.musicAudioSource.Stop ();
}
public void Mute() {
control.musicAudioSource.volume = 0;
}
}
Your answer
Follow this Question
Related Questions
How to stop the main music and play the GameOver music when a player dies? 2 Answers
c# sound in unity 5 1 Answer
How Do I Make A Rising Piano Death Noise? 1 Answer
Music Manager singleton change song 0 Answers
Unity Sound Glitched up (in 2D) 0 Answers