- Home /
Go Back Button Script
I've create a game menu, a pause menu and an instruction screen and on the instruction screen I have an an arrow that 'goes back,' however at the moment the arrow has a script that has 'Application: loadlevel' setting in it and it loads back to the main menu only.
I need a script so that when I click the arrow it goes back to the previous 'setting' instead of it loading the menu; when I press pause, instructions then the arrow button, I want it to go back to the pause screen.
Thanks
Answer by CHPedersen · May 13, 2011 at 11:01 AM
To implement a "Back"-button you need to introduce some kind of record of where you've just been in the GUI. Unity's GUI system invites the creation of state machine-GUIs where one state might correspond to, say, one window being displayed. So to swap between windows, you have to keep track of the previous states.
Try something like this:
string menuPosition = "MainMenu"; string previousPosition = "";
void OnGUI() { switch (menuPosition) { case "MainMenu": DrawMainMenuScreen(); break; case "Settings": previousPosition = "MainMenu"; DrawSettingsScreen(); break; case "Load Game": previousPosition = "MainMenu"; DrawLoadDialog(); break; .etc .etc .etc default: break; } }
Then in lower windows, like the SettingsScreen(), if your Back-Button is clicked you set menuPosition = previousPosition to cause the next call to OnGUI to draw whatever came before. Keep in mind that the above is extremely simplified - First off, the menuPosition probably shouldn't be a string, but an enum. Secondly, the states should probably enter and leave a Stack datastructure to automate the retrieval of the previousPosition, instead of setting it manually every time.
The key is to introduce state memory. Keep track of where you are and just have been in the GUI.
O$$anonymous$$ thanks, I'll give it a try and work round it.
Your answer
Follow this Question
Related Questions
How to place button inside window same place 1 Answer
Unity backbutton crashes while on first scene 0 Answers
Button back not clickable after rotation animation 1 Answer
Unity UI How to make Multiple Clickable Next / Back Buttons? 3 Answers
Back button issue: when I use back button to close keyboard, it also close the scene. 2 Answers