- Home /
How do i make my game to focus on the IN game menu instead of the gameplay
How do i make my game to focus on the IN game menu instead of the gameplay. When i press escape and my IN game menu opens i want the "game" to focus on the menu and not on my fps player in the background. I mean when i have opened the IN game menu and press change weapon class etc my player fires a shot in the background at the same time.. How do i fix this?
Answer by FlyingHighUp · Oct 14, 2017 at 04:58 AM
Unfortunately there isn't a pretty solution right now; you have to create a flag.
In your character controller, check if the main menu is open. If it is, don't take input from Input.GetKeyDown() etc. If you're using a asset store FPS script, they might have a flag coded in there you can set.
Currently the most elegant way to solve this is to code a facade that replaces the Input class (e.g. Unity's CrossPlatformInput class). Then you can set a universal flag to enable/disable player input. But sometimes that's overkill depending on your situation. Maybe once Unity creates the new input system we can finally put this issue to rest.
Thanks for your help! Can you write here kind of what the code would look like in my character controller to check if the menu is open.
declare:
private bool menuOpen=false;
when menu is opened, add:
menuOpen=true;
when menu is close, add:
menuOpen=false;
check if menu is open, add:
if(menuOpen) return; //return will skip the rest of the function
Glurth is that all i have to write? And what exactly does this mean?
private bool menuOpen=false;
This line defines a variable in your class. Is states that it is of type "bool" which is short for boolean: meaning it can only two possible values: true or false (stored as a bit, this is one or zero respectively). Sometimes this is also called a flag: which can be (up or down).
if(menuOpen) return;
This line will evaluate the value of the menuOpen variable. If true, it will invoke the code inside th if statement. I used a shorthand here to not use the { }, which is legal because there is only one command inside the if statement. It is equivalent to:
if(menuOpen)
{
return;
}
The return command means that the currently running function is done, and should return program control (what command is invoked next) to wherever the function was invoked from.
This is really basic c# stuff, but still, I suspect these answers will just raise $$anonymous$$ORE questions, and so would recommend an online course on c#.
While that is all you have to write, for flyinguphigh's solution, you do need to make sure you put it in the right places.
I guess i can write this in the update function in character controller? Will this code make the gameplay to "stop" while the menu is open?
if you put the if(menuOpen) return;
in your character controller's update function, that's all that will be disabled, physics and other updates would not stop running. (how to do that would be a separate question, but I suspect you can find one on that subject, on this site).
Your answer
![](https://koobas.hobune.stream/wayback/20220612140935im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Game is in paused mode when returning to main menu 0 Answers
Overlaping GUITexture, help! 1 Answer
HighScore analytics 0 Answers
Menu gui with background image 0 Answers