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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by tumpula · Dec 18, 2014 at 01:35 AM · buttontoggle buttonunity4.6mute

How to prevent Unity4.6 Toggle button loosing its state when jumping between scenes?

Hi!

I have a Singleton MusicManager what offers sounds and music to a game, menus, buttons etc. I tested today Unity3d 4.6 new Toggle-button and somehow it is working. If I turn sounds off in my option menu and go to play a game and come back to option menu, the image is back in default state (music on). I am using my MenuManager.cs and it is calling singleton Musicmanager, How can I prevent this? Is there way to tell Unity that don't destroy and instantiate options-scene again when jumping between scenes or could I somehow programmatically change the state/image of the toggle-button to be right?

EDIT: The weirdest thing is that according to the Unity Log, when I come second time to Option screen, it goes automatically MusicManagerSingleton and MenuManager's ToggleMusic() methods even if I haven't touch the whole button? My usecase is that when I go first time to options and mute the music, everything is working fine, BUT when I come back there without touching the button,music starts and mute/sounds Toggle button is in state true.

EDIT2

Unity calls automatically ToggleMusic()-method as well and that was the reason. I added this line and everything started to work: if (toggle.isOn != MusicManagerSingleton.Instance.musicOn())


 public class MenuManager : MonoBehaviour {
 
     public bool musicOn;
     public AudioClip musicClip;
     public UnityEngine.UI.Toggle toggle;
 
 
     public void Start ()
     {
         toggle = GameObject.Find("MusicToggle").GetComponent<UnityEngine.UI.Toggle>();
         toggle.isOn  = MusicManagerSingleton.Instance.musicOn ();
         Debug.Log ("0. START() MenuManager musicOn in ToggleMusic()--->" +toggle.isOn);
     }
 
     public void ToggleMusic() {
         MusicManagerSingleton.Instance.toggleMusic ();
         //toggle.isOn = MusicManagerSingleton.Instance.musicOn ();
         Debug.Log ("1. AFTER TOGGLING MenuManager musicOn in ToggleMusic()--->" +toggle.isOn);
         Debug.Log ("2. AFTER TOGGLING MenuManager musicOn in ToggleMusic()--->" +MusicManagerSingleton.Instance.musicOn ());
     }
 
 }
 
 EDIT2 NOW THIS IS WORKING
 ***************MenuManagerSingleton.toggleMusic()*************************

public void ToggleMusic() {

     if (toggle.isOn != MusicManagerSingleton.Instance.musicOn()) {
         MusicManagerSingleton.Instance.toggleMusic ();

     } 

 }


Thanks! Tumpula

[1]: /storage/temp/37218-screen+shot+2014-12-18+at+11.37.43.png

screen shot 2014-12-18 at 11.37.43.png (76.6 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HYPERSAVV · Dec 18, 2014 at 01:42 AM

You could use the PlayerPrefs to store state between scenes. When you alter the value in the options, set the value in PlayerPrefs. When you come back to the option menu, get the last recorded values from PlayerPrefs.

Comment
Add comment · Show 4 · 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 tumpula · Dec 18, 2014 at 08:54 AM 0
Share

Hi and thanks for answering! I have the state already in my $$anonymous$$usic$$anonymous$$anager-Singleton, but the problem is that how to tell it to new 4.6 UI Toggle Button so that image is always in correct state as well? No when user clicks the mute button, it calls $$anonymous$$enu$$anonymous$$anager's Toggle$$anonymous$$usic()-method and it calls the $$anonymous$$usic$$anonymous$$anager: public void Toggle$$anonymous$$usic() { $$anonymous$$usic$$anonymous$$anagerSingleton.Instance.toggle$$anonymous$$usic ();

avatar image HYPERSAVV · Dec 18, 2014 at 09:32 AM 0
Share

Think of it this way...your options menu is merely going to represent the current state of $$anonymous$$usic$$anonymous$$anager. So when you "start" the options menu you should just query the necessary values/states and update the UI to reflect that. Your flow should be something like this.

  1. Go to options scene

  2. Update UI elements to reflect state of $$anonymous$$usic$$anonymous$$anager

  3. User does stuff

  4. User signals interest in leaving scene

  5. Update $$anonymous$$usic$$anonymous$$anager to reflect state of UI (if this is not done when interactions are made)

  6. Exit to next scene

This way you are not considered with maintaining a single menu. It's just a view on top of a layer of data. NOTE: Step 2 assumes that you can retrieve the state from $$anonymous$$usic$$anonymous$$anager, so these will need to have public getters. Not sure how familiar you are with C#/OOP, but this can be down like so:

 public bool mute { get; private set; }

The value can still be queried from other objects, but $$anonymous$$usic$$anonymous$$anager will still be in control of altering it.

avatar image tumpula · Dec 18, 2014 at 09:47 AM 0
Share

Hi!

I think that I have all of this. The state is saved on $$anonymous$$usic$$anonymous$$anager and $$anonymous$$enu$$anonymous$$anager asks it from the $$anonymous$$usic$$anonymous$$anager. I updated the question a little bit so could you please check it.

Thanks you! Tumpula

avatar image HYPERSAVV · Dec 18, 2014 at 10:20 AM 0
Share

It seems like you have everything in order here...I'm assu$$anonymous$$g you have something like this for Start:

 // Within $$anonymous$$enu$$anonymous$$anager...
 void Start()
 {
    Toggle toggle = GameObject.Find("TheToggle").GetComponent<Toggle>();
    toggle.isOn = get$$anonymous$$usicOn();
 }

In your code I don't see where $$anonymous$$enu$$anonymous$$anager updates the Toggle at start (you are only doing if after the user makes a gesture), so if you did not omit anything in your sample you are still missing that key part.

avatar image
0

Answer by SkaredCreations · Dec 18, 2014 at 01:40 AM

You can declare a static variable in your MenuManager to store your toggle state before loading the new level and in your MenuManager.Start you will set the toggle state yourToggle.isOn = MenuManager.yourVariable (static variables persist even if you change level).

Comment
Add comment · Show 3 · 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 tumpula · Dec 18, 2014 at 09:05 AM 0
Share

I have the static variable: public static bool musicOn; but when choosing the On Value Changed in Inspector, Unity doesn't show that variable at all. It only shows the Toggle$$anonymous$$usic()-method, not the static parameter at all under the $$anonymous$$enu$$anonymous$$anger. Thanks for answering!

avatar image SkaredCreations · Dec 18, 2014 at 09:57 AM 0
Share

I was not talkimg about changing or setting a method for On Value Changed, please re-read my answer: I'm saying to set "isOn" property of your toggle to "musicOn" directly inside the method Start of $$anonymous$$enu$$anonymous$$anaget (that I suppose is a $$anonymous$$onobehavior)

avatar image tumpula · Dec 18, 2014 at 10:17 AM 0
Share

Good idea!

 public void Start ()
     {
         musicOn = $$anonymous$$usic$$anonymous$$anagerSingleton.Instance.musicOn();
         Debug.Log ("musicOn in START=========>" +musicOn);
     }

I added a start method and in log it says: musicOn in START=========>False UnityEngine.Debug:Log(Object) $$anonymous$$enu$$anonymous$$anager:Start() (at Assets/Scripts/$$anonymous$$ain$$anonymous$$enu/$$anonymous$$enu$$anonymous$$anager.cs:12)

but in the game screen:

alt text

screen shot 2014-12-18 at 12.12.32.png (28.2 kB)
avatar image
0

Answer by Kiwasi · Dec 18, 2014 at 01:57 AM

There are multiple ways to approach this. Choose the one that fits. In the order I would suggest they are:

  • Have the button check the sound manager state in OnEnable

  • Use DontDestroyOnLoad to maintain the same button

  • Tie an event to the change in your sound manager. Update the button every time the event fires

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 tumpula · Dec 18, 2014 at 09:08 AM 0
Share

Could you help me a little bit, I have no idea how to do that :) I am new with unity. $$anonymous$$y ToggleButton name is $$anonymous$$usicToggle in inspector. Thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs for Mute/Unmute button 1 Answer

GameObject as toggle button with touch 1 Answer

Toggle Button State On/Off 2 Answers

GUI.Button Toggling Error 1 Answer

Toggle button GUI texture not changing on click? 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