- Home /
The question is answered, right answer was accepted
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
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).
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.
how to do that? i used this code into prefab and add this into all scenes except gameplay scene. @Andres Fernandez
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.
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.
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);
}
}
Follow this Question
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