The question is answered, right answer was accepted
Object missing after changing scenes
Hey Guys,
i'm trying to add a "Mute"-Button to my game. the button handles the GameObject "Music Player" which has the following script:
using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour
{
public static MusicPlayer instance = null;
private static bool pause = false;
void Awake ()
{
if (instance != null) {
Destroy (gameObject);
Debug.Log ("Duplicate Selfdestuct");
} else {
instance = this;
GameObject.DontDestroyOnLoad (gameObject);
Debug.Log ("Sound played");
}
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (pause) {
audio.Pause ();
}
}
public void PauseMusic ()
{
//check for audio component first
if (!pause) {
//if audio is playing then pause it, else unpause it
pause = true;
Debug.Log ("Audio is Paused");
} else {
pause = false;
audio.Play ();
Debug.Log ("Audio is Unpaused");
}
}
}
So my buttons on Click () ist adjusted to MusicPlayer.PauseMusic and all work fine. Music is (un)muted every time i click - behavior is as i wish.
BUT if i change scenes the music player destroy itself (because i want persistent background music) and button is missing it's previous Game Object.
What can i do?
Greetings
need clarificiation: it sounds like the problem is the class (not shown in code) that does your mute button, looses it's reference to the $$anonymous$$usicPlayer? Is this correct?
If "pause" is a member of $$anonymous$$usicPlayer; why isn't "$$anonymous$$ute"?
IT does loose reference. The "$$anonymous$$ute"-Button is a Game Object IT does not have a particular class.
Answer by Elunius · Sep 13, 2016 at 03:21 PM
I figured it our myself by making a new script and adding it to the mute button using UnityEngine; using System.Collections;
public class Mute : MonoBehaviour
{
private MusicPlayer mp;
void Start ()
{
mp = GameObject.FindObjectOfType<MusicPlayer> ();
}
void Update ()
{
}
public void PauseMusic ()
{
//check for audio component first
if (!MusicPlayer.pause) {
//if audio is playing then pause it, else unpause it
MusicPlayer.pause = true;
mp.audio.Pause ();
Debug.Log ("Audio is Paused");
} else {
MusicPlayer.pause = false;
mp.audio.Play ();
Debug.Log ("Audio is Unpaused");
}
}
}
Answer by ArturoSR · Sep 12, 2016 at 01:16 AM
@Elunius Hello there.
OK, the only thing you need to do it's use a method or set in the method where you load you new scene to do not destroy the selected game object, I made this some time a go, just like you I did a button to mute on/off an audio component (background music), so, when you go to the next level the music goes on, if you are trying to keep some elements from one scene into another, this is the best way to achieve that, remember, when you are loading the next scene, cheers.
Have a look in my Level$$anonymous$$anager:
using UnityEngine;
using System.Collections;
public class Level$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public void LoadLevel (string name)
{
Application.LoadLevel (name);
}
public void QuitRequest ()
{
Application.Quit ();
}
public void LoadNextLevel ()
{
Application.LoadLevel (Application.loadedLevel + 1);
}
}
i acutally achieved DontDestroyOnLoad in my $$anonymous$$usicPlayer. Do you mean i need my button don't be destroyed? oO Actually id don't understand what you trying to say ^^'