How can i make pause menu

i want to make a 2d game in android using C# can any one help me plz ?

2 Answers

2

Hello Hajamat. A pause menu is rather simple. Some code off the top of my head would be this:

bool pause=false;

Void Start(){
     
}
Void Update(){
    if(pause){
         //Disable Movement Scripts Example:
         GetComponent<PlayerMovement>().enable=false;
     }else {
         //Enable Movement Scripts
         GetComponent<PlayerMovement>().enable=True;
     }
}
void OnGUI(){
     GUI.skin.label.fontSize=Screen.height/50;
     if(GUI.Button(new Rect(Screen.width/20,Screen.height-Screen.height/10,Screen.width/20,Screen.height/20),"Pause")){
          pause=true;
}
if(pause){
     //Draw pause menu stuff.
}
}

Thats just off the top of my head so there might be errors, But it should work.

the simpliest way to pause the game is Time.timescale = 0; this will Pause the game but if you press any keys while doing this once you set the Time.timescale = 1; the code from the keys will execute so the way i usally do this is in my GameManager Script thats easyli accesible from all scripts i make a bool called Pause and check on all Scripts that has Key press commands

Example

 if (FindObjectOfType<GameManager>().Pause != true && getKey(keycode.Space))
{
// execute code
}
else 
{
// do nothing the game is pasued
}