Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by aceofpack · Feb 05, 2016 at 08:29 PM · soundmusicsingletoninstances

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;
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

40 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges