Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 awplays49 · Feb 11, 2015 at 01:28 PM · menuintmenu screen

Make an int switch?

Im making a menu system and every time the player presses escape, i want the an int called currentPanel to go down, so it knows when the menu is closed. however, when the menu is at -1, i want it to open the first panel rather than closing it.

so heres what would happen if you had the 4th menu panel open and pressed esc 5 times:

  1. move to 3.

  2. move to 2.

  3. move to 1.

  4. move to 0.

  5. close menu.

  6. open menu 0.

i tried

 if (Input.GetKeyDown (KeyCode.Escape))
         {
             if (CurrentPanel < 0)
             {
                 CurrentPanel ++;
             }
             if (CurrentPanel == 0)
             {
                 CurrentPanel --;
             }

but it doesnt work because when one switches, the other switches.

Comment
Add comment · Show 3
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 chariot · Feb 11, 2015 at 01:33 PM 0
Share

Why not http://www.w3schools.com/js/js_switch.asp ?))

avatar image meat5000 ♦ · Feb 11, 2015 at 01:50 PM 0
Share

A switch statement is good for this. However, don't assume that Javascript learned for w3schools is going to work in Unity; it doesn't support web Javascript. Ins$$anonymous$$d its a mashup of javascript actionscript and some other nonsense (which all together works quite well).

avatar image awplays49 · Feb 11, 2015 at 01:53 PM 0
Share

Sorry to bother again, but what is the difference between @alfalfasprout answer and my code?

2 Replies

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

Answer by Alanisaac · Feb 11, 2015 at 01:38 PM

Just need an else before the if as below. Otherwise, both statements will execute, and you're back at -1.

     if (Input.GetKeyDown (KeyCode.Escape))
     {
         if (CurrentPanel < 0)
         {
             CurrentPanel ++;
         }
         else if (CurrentPanel == 0)
         {
             CurrentPanel --;
         }
     }
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 awplays49 · Feb 11, 2015 at 01:43 PM 0
Share

Cool, thanks!!!

avatar image
0

Answer by VesuvianPrime · Feb 11, 2015 at 01:38 PM

A switch like you're asking for would be:

 switch (CurrentPanel)
 {
     case -1:
         CurrentPanel++;
         break;
 
     case 0:
         CurrentPanel--;
         break;
 
     // and so on
 }

For a more robust menu system I would implement something like the following:

 public enum Menu
 {
     Main,
     Options,
     NewGame,
     Etc
 }
 
 private Stack<Menu> m_MenuStack;
 
 public void GoToMenu(Menu menu)
 {
     m_MenuStack.Push(menu);
 }
 
 public void GoToPreviousMenu()
 {
     m_MenuStack.Pop();
     if (m_MenuStack.Count == 0)
         GoToMenu(Menu.Main);
 }
 
 public void Update()
 {
     if (Input.GetKeyDown(KeyCode.Escape))
         GoToPreviousMenu();
 }
 
 public void OnGUI()
 {
     Menu current = m_MenuStack.Peek();
 
     switch (current)
     {
         case Menu.Main:
             // Draw the main menu
             break;
 
         case Menu.Options:
             // Draw the options menu
             break;
 
         // etc
 
         default:
             throw new System.ArgumentOutOfBoundsException();
     }
 }


Comment
Add comment · Show 2 · 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 TheHemohscinProject · Apr 10 at 05:24 AM 0
Share

unity just gives an error, apparently using ints is invalid in switch statements

avatar image Caeser_21 TheHemohscinProject · Apr 10 at 05:44 AM 0
Share

What error are you getting?

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you make a pause menu? 0 Answers

How do i add an extra menu to my main menu (I'm a Beginner programmer!) 1 Answer

Settings Screen Help 1 Answer

How do i create a Menu that has different Tabs without switching to different Scenes? 2 Answers

2D Start Background fill the screen 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