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

154 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 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 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 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

Singleton Problem 0 Answers

error CS0176: Static member `GameManagerSingleton.score' cannot be accessed with an instance reference, qualify it with a type name instead 1 Answer

Singleton HELP 1 Answer

How to make Unity singleton base class to support generics? 1 Answer

How can I handle scripts correctly? What's a good way to apply scripts to GameObjects? 1 Answer


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