How to go back to a previous state?

Hi everyone. Basically, I’d like to create a soft of character status/menu state accessible at any point in the game by pressing a certain key. I figured out how to do that, but the problem is that I don’t know how to return it to the state it was at before. How would I go about doing this?

Thanks in advance. :slight_smile:

1 Like

Hi @Sarah_Lyn, what is the purpose of returning the player to a previous state? e.g. is it if they get killed in your game and you want them to have another go/life or something else? There are probably several approaches but choosing one may depend on what you are trying to achive.

I’m very rusty in unity as been a while since i came though the course but i think i know what you are trying to do.
You have a menu UI that for example when you press escape it pops up.
What you want is for it to close again when you press escape again.

To do this you need to have the code recognise if the UI is open or not.
http://answers.unity3d.com/questions/1094886/open-and-close-ui-menu-on-background-mouse-click.html

Something like this might get you on your way into how to achieve it.

Okay, so to be sure I understand this right, I’ll need a new script for a menu UI? And will anything I need to display from the other script need to be public rather than private? (for example, objects in the character’s inventory) I’m still not quite sure what all the differences between the two are.

Sarah, I think that the best way to do it is to create an prefab that pops up an whole menu when instantiated. You can use Time.timescale to pause the game too.

1 Like

This may already be effectively solved. But since it’s open I wanted to throw in my 2 cents. I would handle this by setting a global variable that stores the current state when the key in pressed to view the character sheet before changing myState.

If (Input.GetKeyDown(KeyCode.C) { 
 //"C" key opens character sheet
 CurrentState = myState;
 myState = CharacterSheet;
}

Then use that variable to return to the previous state when exiting the character sheet from the character sheet state.

If (Input.GetKeyDown(KeyCode.E) { 
 //"E" key exits character sheet
 myState = CurrentState;
}

This doesn’t require reaching very far outside what’s in the course up to this point. It would only be creating a global variable to use for tracking what the state is when the player enters the character sheet.

1 Like

Privacy & Terms