- Home /
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
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.
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;
}
}
Thanks your script is brilliant but im gonna go with the else if solution because its easier to call in other scripts.
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
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;
}
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
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)
Your answer
Follow this Question
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