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 /
This question was closed Sep 13, 2016 at 03:22 PM by Elunius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Elunius · Sep 10, 2016 at 05:12 PM · buttondestroybackgroundunity 4.6music

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

Comment
Add comment · Show 2
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 Glurth · Sep 10, 2016 at 05:26 PM 0
Share

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

avatar image Elunius · Sep 10, 2016 at 07:07 PM 0
Share

IT does loose reference. The "$$anonymous$$ute"-Button is a Game Object IT does not have a particular class.

2 Replies

  • Sort: 
avatar image
1
Best Answer

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");
         }
     }
 }
Comment
Add comment · 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
0

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.

Comment
Add comment · Show 1 · 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 Elunius · Sep 12, 2016 at 08:33 PM 0
Share

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

Follow this Question

Answers Answers and Comments

79 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

Related Questions

I want to Instantiate a force field - let it work for x(sec) show countdown and then make wait for a count up - but then my button doesn't seem to work anymore? 0 Answers

Music drags / lags while loading things 1 Answer

How do i turn on/off background music and keep the changes when reload the scene 1 Answer

UI: On pointer down enter detection 0 Answers

I can't press my buttons please help! 0 Answers


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