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 Forbidden_Duck · Feb 04, 2017 at 01:04 AM · musicduplicatepongbackground music

Background Music Error!

Hi,

I am having trouble with my Background music. I assigned it to loop and not to delete on load but when you get back to the Main Menu it starts up again. I tried fixing it but I kept getting errors this is the code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class NotDeleteMusic : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         // Identify your scene where you have to stop current running music and probably start new one
         if (SceneManager.sceneLoaded += "MainMenu")
         {
             // Find the game object who is playing the audio and STOP audio clip.
             GameObject.FindGameObjectWithTag("GameMusic").GetComponent<AudioSource>().Stop();
         }
         DontDestroyOnLoad(this);
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HenryStrattonFW · Feb 04, 2017 at 11:20 PM

You're error is because your if statement is invalid. SceneManager.sceneLoaded is an event you subscribe to, not a condition, replacing that for a proper event subscription should give you the desired results.

 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class NotDeleteMusic : MonoBehaviour
 {
     void Start()
     {
         SceneManager.sceneLoaded += _SceneLoaded;
         DontDestroyOnLoad(this);
     }
 
     private void _SceneLoaded(Scene lScene, LoadSceneMode lMode)
     {
         if (lScene.name.Equals("MainMenu"))
         {
             GameObject.FindGameObjectWithTag("GameMusic").GetComponent<AudioSource>().Stop();
         }
     }
 }
Comment
Add comment · Show 7 · Share
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
avatar image Forbidden_Duck · Feb 04, 2017 at 11:46 PM 0
Share

The music still duplicates when you go to the $$anonymous$$ain $$anonymous$$enu

avatar image Forbidden_Duck · Feb 07, 2017 at 04:38 AM 0
Share

It still doesn't work

avatar image HenryStrattonFW Forbidden_Duck · Feb 07, 2017 at 09:44 AM 0
Share

I've just a a thought. Is the object with this script on part of the $$anonymous$$ain$$anonymous$$enu scene? if so the reasons it will duplicate is that you mark the object as not to be destroyed, but if you then re-load the scene it came from a new instance of this object will be created.

This might explain why you have a duplicate, in which case you will need to upgrade your script to check if this music player already exists, and delete the duplicate on creation (essentially a self-enforcing singleton).

If not please zip up your project and email the zip or a dropbox link or something and I'll try to take a look at this after work to get to the bottom of it. henrystrattonfw@gmail.com

avatar image Forbidden_Duck HenryStrattonFW · Feb 07, 2017 at 11:37 AM 0
Share

The music is gameobject on the $$anonymous$$ain$$anonymous$$enu scene. I don't understand what I am suppose to do sorry.

Show more comments
avatar image Forbidden_Duck · Feb 08, 2017 at 12:47 AM 0
Share

m_instanceexist doesn't exist in the current context

avatar image HenryStrattonFW Forbidden_Duck · Feb 08, 2017 at 11:12 AM 0
Share

Apologies there was a missed keyword in that code where I was rushed.

      private static m_InstanceExists = false;

should be

      private static bool m_InstanceExists = false;


But you need to be able to spot this kind of error, always double check the code people give you as non of us are perfect :P debugging is one of the best skill you can have.

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

92 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

Related Questions

Where to find free background music? 11 Answers

Does anyone know a good place to download free background music for games? Do most people just make their own music? 1 Answer

How to stop all application music playing in the background. 1 Answer

How do I save the background music in the settings? 0 Answers

Several music tracks on camera controlled by AudioSource.mute 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