Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How do I call a GUI on more than 1 scene?

Discussion in 'Game Design' started by AJMarley, Jul 3, 2019.

  1. AJMarley

    AJMarley

    Joined:
    Jul 3, 2019
    Posts:
    3
    I am very new to Unity and C#, just started about a week ago and have run into something that I do not know how to make for my final idea. My idea was to have a Main Menu when you start the game and a settings menu as well. I made that perfectly fine, however I need the settings menu to also be accessible to the player in the pause menu and all other levels in the game so they don't have to leave the game just to edit there settings. Like I said before I am very new to Unity and Coding C# in general. If anyone could help me that would be super helpful.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,515
    There's two main ways to approach this. You can make the settings menu its own prefab, where you instantiate it in any scene in which you need the settings menu. Alternatively, you can make the settings menu its own separate scene which you additive load alongside any main scene which needs access to the settings menu.

    I typically go the prefab route for a settings menu, but I don't think either strategy is inherently superior to the other.
     
  3. AJMarley

    AJMarley

    Joined:
    Jul 3, 2019
    Posts:
    3
    Perfect that works for what I need, but another problem came up. Okay so I have the GUI's in another scene so I can load that scene whenever I hit the "Settings" button, but sense I have 2 buttons from different scenes that load the GUI scene I need to be able to tell another button to go back to a specific scene. Its kinda hard for me to explain this in a unity stand point, but what I'm trying to do is to find the scene that I hit one of the "settings" buttons on and when I hit a "back" button of some sorts it would take me back to that exact same scene so I wouldn't go to the main menu or wherever the second "settings" button I have. Like if you had a main menu that had a settings button that goes to a GUI's specific scene, and another button on the pause menu in the actual game that also goes to that GUI's specific scene how would you differentiate between both scenes so that when I hit the settings button in the main menu and back button on the GUI's scene that it wont take me to the game itself but back to the main menu.
     
  4. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    534
    You could store the previous scene name in a string on a persistent object.
    You can create a persistent object (one that stays alive when changing scenes) by using DontDestroyOnLoad:
    https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

    Create a string in this object called "PreviousSceneName", and before you load a new scene, set PreviousSceneName to the current scene name.
    Then when you click your "back" button, you would call LoadScene(PreviousSceneName)

    One pitfall: This would only work once, because if you had a back button in your "main menu" and in your "settings menu", the back button would just keep taking you between the two.

    If you wanted to get around that scenario, and have your "back" button working more like a browsers back button, you'd have to look at storing the previous scenes in an array and working your way back through them - but I don't think you're asking for that!
     
  5. AJMarley

    AJMarley

    Joined:
    Jul 3, 2019
    Posts:
    3
    Perfect that's exactly what I'm asking for! Thank you Joe-Censored and Serinx for the help.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,515
    I'd recommend against just loading a separate GUI scene and then returning to the previous scene for something like a settings menu. I'd additive load the GUI scene alongside the current scene, so you're running both at the same time. Then the settings menu can open without interrupting the game (though you could still pause the game if you want).
     
    frosted likes this.
  7. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    3,648
    Yeah, I use a mix of the two approaches. I do small popups as prefabs and more complex stuff as additive scenes.

    So I have a HUD scene and 'unit detail' popups and stuff as prefabs.
     
    Joe-Censored likes this.
  8. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    670
    I use a mix of approaches. I tied the GUI to the player character, which is persistent between scenes. The advantage to this is that it allows me to have multiple GUIs if I have splitscreen gameplay.
     
    Joe-Censored likes this.