Switch Camera to a Menu

Hi there,

In my game when I press “Escape”, my game is paused. How can I get to another camera that would look to my menu with button and stuff.


Why are you trying to switch cameras to display a menu? The menu does not need to exist in 3D space. GUI elements are visible based on screen coordinates, not position relative to the camera.

If you want to display a camera that is not currently visible you could use the camera.enabled feature to set the selected camera to visible/not visible (true/false). This “seems” like what you are trying to do, but as I stated above it may not be what you actually want to do.

If for some reason you had to actually switch the camera to a new target there are many ways to go about that. You could try attaching a script to your main camera that checks for your pause event by looking for either the “esc” key press or some toggle available in another one of your scripts that manages the pause state. Once one of these conditions has been met then the camera will know that the game is paused and you can do whatever needs to be done (load a menu, play some animation, whatever).

Keep in mind that if you are checking for the key press and not a toggle from another script you will want to check for the input in your Update() function and not the OnGUI() function. OnGUI can run multiple times per frame and will potentially toggle the menu on/off with one keypress. Simply look for the action in Update(), flip a boolean and check if it has been flipped in OnGUI in order to determine if you should switch cameras.