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 /
avatar image
0
Question by ryangallion · Mar 20, 2018 at 06:33 PM · uiscene-switchingpersistent

UI Menus and Multiple Scenes - Pass variables?

Hey guys,

I'm an artist so don't do much programming, but I'm stuck on what I believe is a simple problem. I've spent the morning reading up on static variables vs. dontdestroy vs. singleton, and I feel like my head is going to explode.

So anyway I have a simple intro menu that loads when the game starts. Upon closing that, I add a simple button in the corner that just shows the menu again. When the user jumps to a different scene, I just want that "state" to be correct, so when they come back they would see the minimized menu button, not the initial full screen menu.

At first I felt like this would be so simple, but with every technique and post I read, I just confuse myself more and more.

So what's the easiest method to achieve this? (I don't want to use PlayerPrefs because I do want the initial screen when they reload the game) It's a UI button that will ultimately "set" that scene state, since closing that is what bring up the smaller menu button.

Thanks, any help would be appreciated...

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by FuzzyLogic · Mar 20, 2018 at 07:05 PM

The best option is to store your menu state in a static class variable which will persist as long as your game is running.

When the menu is closed, set PersistentGameState.showMenu = false When the menu is opened, set PersistentGameState.showMenu = true

When the scene changes, the menu object will be reset as usual and you can use the showMenu state to determine if you should display the menu or hide it.

something like... PersistentGameState.cs

 public static class PersistentGameState {
     public static bool showMenu = true; // initially the menu should be displayed
 }

MenuState.cs (attach this to your menu object)

 using UnityEngine;
 
 public class MenuState : MonoBehaviour {
 
     private void Start() {
         // when the scene is loaded, restore the persistent menu state
         DisplayMenu( PersistentGameState.showMenu );
     }
 
     public void DisplayMenu( bool state) {
         PersistentGameState.showMenu = state;
         this.gameObject.SetActive( state );
     }
 
 }

From your 'open menu' UI button, you can hook up the OnClick event to the Menu object's MenuState->DisplayMenu() method. Make sure that you check the box for the OnClick parameters so that it is calling DisplayMenu(true). If you have a 'close menu' button, you would have it hooked up to call DisplayMenu(false).

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 ryangallion · Mar 20, 2018 at 07:41 PM 0
Share

Thanks for the reply, this looks promising, and simple as I had hoped.

Just a few quick questions regarding the implementation:

Am I applying the PersistentGameState script to a GameObject? (I thought I read somewhere that you can't do that with a static variable? But again this just might just show how confused I am lol)

Then the second piece, this just gets attached as a separate script to my UI button, like On$$anonymous$$ouseDown ()?

Thanks $$anonymous$$an.

avatar image FuzzyLogic ryangallion · Mar 20, 2018 at 11:28 PM 0
Share

I updated my answer for you, to be more explicit.

avatar image ryangallion FuzzyLogic · Mar 21, 2018 at 01:10 PM 0
Share

Awesome, thanks dude. I really appreciate it. Its working, now I just have to figure out how, lol.

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

130 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 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

Persistent Scene 2 Answers

after click a GUI button from Menu to Game there are two screens 1 Answer

Worldspace UI in wrong position/scale after scene change 0 Answers

UI canvas or panel that only appears once per game session? 1 Answer

How do I hook up references using multi-scene editing? 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