Depends on your implementation.
I would do something like this, using this menu flow for the example:
Main Menu -> Mode Select -> Lobby -> Start Game
If you're not using some kind of Menu Manager, then things can get a bit messy. If I was designing a Menu Manager I would have the manager keep a reference to the last active menu when going to a new one, but for the sake of this example I'm going to assume that you're just going menu object to menu object.
Each menu object would need to have a reference to both its next and previous menu (if applicable). So, for example, the Lobby menu would have a reference to both Start Game as its next menu and Mode Select as its previous menu.
From there it would be as easy as grabbing the next or previous menu before you transition.
In code that could look something like this:
Code:
//Declared variables that are set in the Inspector (which is why they're null here).
public GameObject NextMenu = null;
public GameObject PreviousMenu = null;
public void TransitionForward()
{
//Deactivate our active menu object (which is this gameObject) and activate our NextMenu.
this.gameObject.SetActive(false);
this.NextMenu.SetActive(true);
}
public void TransitionBack()
{
//Deactivate our active menu object (which is this gameObject) and activate our NextMenu.
this.gameObject.SetActive(false);
this.PreviousMenu.SetActive(true);
}
Something like that, but probably with an actual transition instead of just deactivating/activating GameObjects. Note that the use of "this" isn't needed, I just added it to make the code a bit more verbose and easier to understand/read.