- Home /
While a menu is open. witch is the better way to don't allow other scrips get key codes?
The problem is that when i close the menu with left click, the wepon equiped shoots.
pd: With "key codes" I mean keyUp(), KeyDown() ....
Answer by Cornelis-de-Jager · Jul 10, 2019 at 09:59 PM
It's best to have a controller script that controls which inputs are allowed or not. Commonly the Game-State pattern is used for this.
States:
- Default
- InGame
- InMenu
- Paused
enum GameState {
Default,
InGame,
InMenu,
Paused
}
// Game state variable
public GameState gameState = GameState.Default;
public GameObject UI;
public GameObject Game;
void Update () {
switch (gameState){
case (GameState.Default):
UI.GetComponent<UIController>().Show();
Game.GetComponent<GameController>().Reset();
return;
case (GameState.InGame):
UI.GetComponent<UIController>().Hide();
Game.GetComponent<GameController>().Play();
return;
case (GameState.InMenu):
UI.GetComponent<UIController>().Show();
Game.GetComponent<GameController>().Pause();
return;
case (GameState.Paused):
UI.GetComponent<UIController>().Hide();
Game.GetComponent<GameController>().Pause();
return;
}
}
if (GameState == GameState.InGame){
}
Answer by Magso · Jul 10, 2019 at 09:41 PM
Set a bool variable to true when the menu is open and in the input's if statements add if((Input) && !menuBool){
to check the menu isn't open. When the menu is closed start a coroutine and have the bool wait a frame or millisecond before setting it to false.
Your answer
![](https://koobas.hobune.stream/wayback/20220612204656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I'm having animation problems 0 Answers
Multiple key. Press order 0 Answers
animations not working correctly in build 1 Answer
Perform event only once on keypress 1 Answer
Write custom input manager 0 Answers