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 /
avatar image
1
Question by Reeceg · Jul 28, 2014 at 02:43 AM ·

Need code help from smart people

Hi my code is meant to activate a different guis for a pause menu but its not working

 #pragma strict
 static var top = true;
 static var middle = false;
 static var bottom = false;
 
 function Update(){
 if (top && Input.GetKeyDown (KeyCode.DownArrow))
 {
 top = false;
 middle = true;
 }
 if (middle && Input.GetKeyDown (KeyCode.DownArrow))
 {
 middle = false;
 bottom = true;
 }
 if (bottom && Input.GetKeyDown (KeyCode.DownArrow))
 {
 bottom = false;
 top = true;
 }
 if (top && Input.GetKeyDown (KeyCode.UpArrow))
 {
 top = false;
 bottom = true;
 }
 if (middle && Input.GetKeyDown (KeyCode.UpArrow))
 {
 middle = false;
 top = true;
 }
 if (bottom && Input.GetKeyDown (KeyCode.UpArrow))
 {
 bottom = false;
 middle = true;
 }
 }

the top to middle only works when i press up and the middle to top works with both up and down and nothing else works

Comment
Add comment · Show 1
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 SirCrazyNugget · Jul 28, 2014 at 03:04 AM 0
Share

That's a lot of comparisons you're doing which could really be tidied up, it's kind of hiding the fact the comparisons are falling through each other and nearly all of them returning true after it's now been set.

Presume top is true and $$anonymous$$eyCode.DownArrow is true. After you've checked if top on line 7, middle is now true.

The next comparison on line 12 will now return true as you've just set middle to be true, the same applies throughout your code.

If it can only ever be top, middle or bottom don't store these as bools, store them as an int or better still as an enum.

1 Reply

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

Answer by Nition · Jul 28, 2014 at 02:59 AM

Your logic actually looks sound, but I think what you've missed is that more than one of those If statements can trigger at once. For instance if you're at Top and you press down, the first If triggers and now you're at middle, but then the code for the second If runs as well, and you're still set as pressing the down arrow this frame, so that code runs too. You can probably fix your version just by making all but the first If into Else If.

Here's a less fiddly solution that works, doing basically the same thing as you, but using an enum instead of the three bools and splitting out some of the logic into separate methods:

 #pragma strict
 
 enum MenuState { Top, Middle, Bottom }
 var menuState = MenuState.Top;
 
 function Update() {
     if (Input.GetKeyDown(KeyCode.DownArrow)) {
         MenuDown();
     }
     else if (Input.GetKeyDown(KeyCode.UpArrow)) {
         MenuUp();
     }
 }
 
 function MenuDown() {
     switch (menuState) {
         case MenuState.Top:
             menuState = MenuState.Middle;
             break;
         case MenuState.Middle:
             menuState = MenuState.Bottom;
             break;
         default:
             menuState = MenuState.Top;
             break;
     }
 }
 
 function MenuUp() {
     switch (menuState) {
         case MenuState.Top:
             menuState = MenuState.Bottom;
             break;
         case MenuState.Middle:
             menuState = MenuState.Top;
             break;
         default:
             menuState = MenuState.Middle;
             break;
     }
 }
Comment
Add comment · Show 7 · 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 Reeceg · Jul 28, 2014 at 03:30 AM 0
Share

Thanks your script is brilliant but im gonna go with the else if solution because its easier to call in other scripts.

avatar image flaviusxvii · Jul 28, 2014 at 03:40 AM 1
Share

Translated

"Thanks for the nice clean code Nition, but I'm going to stick with doing it that other terrible way cause I don't want to bother with learning what you've actually done" -- Reeceg

Translated

avatar image Nition · Jul 28, 2014 at 03:46 AM 0
Share

Haha, thanks flavius. Reeceg, if you want to set the menu to a specific selection from another script, just add a method like:

 function Set$$anonymous$$enu($$anonymous$$enuState newState) {
   menuState = newState;
 }
avatar image Reeceg · Jul 28, 2014 at 04:11 AM 0
Share

fist of all i was trying to use nitions code but i couldn't do this if ($$anonymous$$enu.menuState.Top === false) and second of all screw you flaviusxvii what do you know i haven't even posted the second script and how would you know to what extent i learned about nitions code

avatar image flaviusxvii · Jul 28, 2014 at 04:53 AM 1
Share

You appealed for help from "smart people".. and then you ignored the vastly superior code you were handed. It's asinine.

but i couldn't do this if ($$anonymous$$enu.menuState.Top === false)

You mean something like this..

 if($$anonymous$$enu.menuState != $$anonymous$$enu.$$anonymous$$enuState.Top)
Show more comments

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Why does my var speed say it is wrong 2 Answers

BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers

Failed to update Unity Web Player 0 Answers

How can i make the ray cast don't go through the walls 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