I’m trying to get a menu to appear when a player presses a button on another menu. For example, if the player presses “Options” on the main menu, the main menu will disappear and the Options menu will appear. I can already make GUI items disappear by changing the alpha channel to 0.0f. It doesn’t disable the functionality of the menus, but I have an animation that moves the GUI off screen.
The main issue is with making the GUI (dis)appear when I press another button. I’ve already tried placing the GUI element I want to show in an if statement for a button. It didn’t seem to show anything when I pressed the button. I at least know that placing a button as the if statement’s conditions, since my debug prompts printed a message out to the console. However, I don’t know of any other ways to pull this off.
First make an enum or a string that shows in which menu you are now, and then change it when you need:
string curMenu = “Main”;
void Update()
{
if(curMenu == “Main”)
{
show the main menu with all the buttons
if you press a button change the curMenu to “Options”
}
else if(curMenu == “Options”)
{
show the options menu with all the buttons
if you press “back” change the curMenu to “Main”
}
}
Also if you need you can do something like that(it will only display the button if the TheButtonCanAppear is true, if it’s false it will exit right-away):
I thought you couldn’t interact with buttons outside of the OnGUI() function. Every time I’ve tried to make something happen with a button outside of that function, the button doesn’t even appear, let alone work.
It worked!
I didn’t think enums would make switch between GUI elements like that. That’s extremely useful. Thank you.
However, the elements seem to snap into place; I wanted my menus to slide into place. I’ve already made a function that can do that. Basically, it checks if the elements are in between pre-defined boundaries on their left and right and slides the elements to either boundary when the space button is pressed.
Here’s the code for it:
//moves the GUI around the screen
void AnimateGUI() {
if (Input.GetKey(KeyCode.Space) )
{
//makes movement speed of elements and alpha channel change speed into negative values
if (isMovingLeft == true)
{
boxSpeedX = -5.0f;
alphaSpeed = -Time.deltaTime;
}
//makes movement speed of elements and alpha channel change speed into positive values
else
{
boxSpeedX = 5.0f;
alphaSpeed = Time.deltaTime;
}
}
//sets GUI elements to left boundary value if it passes over it
//also makes elements not visible
if (isMovingLeft == true boxPosX <= boxLimitLeftX)
{
boxPosX = boxLimitLeftX;
boxSpeedX = 0.0f;
alphaNum = 0.0f; //alpha channel is now 0.0f, making elements invisible
alphaSpeed = 0.0f;
isMovingLeft = false;
}
//sets GUI elements to right boundary value if it passes over it
//also makes elements visible
if (isMovingLeft == false boxPosX >= boxLimitRightX)
{
boxPosX = boxLimitRightX;
boxSpeedX = 0.0f;
alphaNum = 1.0f; //alpha channel is now 1.0f, making elements visible
alphaSpeed = 0.0f;
isMovingLeft = true;
}
}
}