Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jul 02, 2014 at 12:36 PM by Prasanna for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Prasanna · Jul 02, 2014 at 08:27 AM · c#scenesingletonsounds

Singleton Problem

Hello there, I have used Singleton Music for Menu. But it keep playing through Gameplay, i don't want to play that Singleton Music for my Gameplay. How can i destroy that?

Here is the Singleton Code..

 using UnityEngine;
 using System.Collections;
 
 public class MyUnitySingleton : MonoBehaviour 
 {
     private static MyUnitySingleton instance = null;
     public static MyUnitySingleton Instance 
     {
         get 
         { 
             return instance; 
         }
     }
     void Awake() {
         if (instance != null && instance != this) 
         {
             Destroy(this.gameObject);
             return;
         } 
         else 
         {
             instance = this;
         }
         DontDestroyOnLoad(this.gameObject);
     }
 }

-Prasanna

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

  • Sort: 
avatar image
2
Best Answer

Answer by Andres-Fernandez · Jul 02, 2014 at 08:42 AM

If the object doesn't get destroyed when loading the gameplay scene then the music will keep playing. You can use some controller object to look for this singleton and make it stop the sound at the beginning of the gameplay scene (and play again when loading the menu scene).

Comment
Add comment · Show 10 · 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 Andres-Fernandez · Jul 02, 2014 at 08:45 AM 0
Share

If you are using some persistent objects (like this one to control the music during menu scenes) you are probably using some other persistent object to control game state and stuff like that. You can create a reference to the music object in the persistent object and make it send "play" and "stop" messages to your music controller object.

avatar image Prasanna · Jul 02, 2014 at 08:49 AM 0
Share

how to do that? i used this code into prefab and add this into all scenes except gameplay scene. @Andres Fernandez

avatar image Andres-Fernandez · Jul 02, 2014 at 09:06 AM 0
Share

Just before you call

 Application.Load("GamePlay");

just add something like:

 GameObject mc = GameObject.Find("$$anonymous$$usicController");
 if (mc != null) {
    mc.Send$$anonymous$$essage("Stop", Send$$anonymous$$essageOptions.DontRequireReceiver);
 }


Then add function Stop in the singleton music object that makes the music to stop.

The fastest way to do this would be to have another persistent object with a reference to the singleton music object, but using the GameObject.Find can be done from any object. Just make sure the name is spelled correctly.

avatar image Andres-Fernandez · Jul 02, 2014 at 09:09 AM 0
Share

BTW I don't get the point in spam$$anonymous$$g singleton prefabs. All of them (after the first one) will get destroyed at the beginning of the scene.

avatar image Prasanna · Jul 02, 2014 at 09:16 AM 0
Share

Here is my Next Level Script..

 using UnityEngine;
 using System.Collections;
 
 public class Play_$$anonymous$$enu : $$anonymous$$onoBehaviour 
 {
     public Texture2D Play_Button;
     public Texture2D Play_Hover_Button;
     public AudioClip Button_Click;
 
     void OnGUI()
     {
         Vector3 scale;
         
         float ResolutionX     = 1024;
         float ResolutionY     = 768;
         
         scale.x             = (float)Screen.width/ResolutionX;
         scale.y             = (float)Screen.height/ResolutionY;
         scale.z             = 1;
         
         $$anonymous$$atrix4x4 sv$$anonymous$$at     = GUI.matrix;
         GUI.matrix             = $$anonymous$$atrix4x4.TRS(new Vector3(0,0,0),Quaternion.identity,scale);
     }
 
     public void On$$anonymous$$ouseEnter()
     {
         guiTexture.texture = Play_Hover_Button;
     }
 
     public void On$$anonymous$$ouseExit()
     {
         guiTexture.texture = Play_Button;
     }
 
     public void On$$anonymous$$ouseUp()
     {
         audio.PlayOneShot (Button_Click);
         GameObject $$anonymous$$usic_Control = GameObject.Find ("$$anonymous$$usic");
         if($$anonymous$$usic_Control != null)
         {
             $$anonymous$$usic_Control.Send$$anonymous$$essage ("Stop", Send$$anonymous$$essageOptions.DontRequireReceiver);
         }
         AutoFade.LoadLevel ("Game_Intro", 3, 1, Color.black);
     }
 }

That "$$anonymous$$usic" is Prefab. this is Singleton Script

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$yUnitySingleton : $$anonymous$$onoBehaviour 
 {
     private static $$anonymous$$yUnitySingleton instance = null;
     public static $$anonymous$$yUnitySingleton Instance 
     {
         get 
         { 
             return instance; 
         }
     }
     void Awake() {
         if (instance != null && instance != this) 
         {
             Destroy(this.gameObject);
             return;
         } 
         else 
         {
             instance = this;
         }
         DontDestroyOnLoad(this.gameObject);
     }
 }
Show more comments

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Access Inventory from player 1 Answer

How to set the position of an object in another scene? 1 Answer

Help with loading a scene,Help changing scenes (C~ script) 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