0
\$\begingroup\$

I am making a 2D game for Android. Almost completed the game but now I need a pause game option. I added a pause icon on the top right side of the screen. The icon is a GUI texture. What I did so far :

I made a script which will bring up some buttons (which is not working) and attached it to the GUITexture.This is the script :

#pragma strict


function OnMouseDown() {
   Debug.Log("*Pause Menu Opens*");
   Time.timeScale = 0;

   if (GUI.Button(Rect(10,10,100,50),"Restart"));
      Application.LoadLevel(Application.loadedLevel);

   if (GUI.Button(Rect(10,60,100,50),"MainMenu"));
      Application.LoadLevel("MainMenu");
}

Now the problem stands at the part where the buttons won't show up and the game freezes at the first frame. Please if you can help I would be really thankful.

Edit #1 : I just noticed that when I click "Pause" the game freezes and it takes me to the MainMenu. That's because I added the GUIButton which takes you to the main menu. I think the whole script structure is wrong. I also forgot to mention that I am new to scripting/unity.

Thank you.

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

The problem here is that you're trying to access the GUI functions from outside the OnGUI function. You'll want to set some sort of variable indicating that the game is paused in the input event handler, and then alter the game's behaviour in the OnGUI function, like this:

function OnMouseDown(){
    this.paused = !this.paused;
}

function OnGUI(){
    if(this.paused){
        if (GUI.Button(Rect(10,10,100,50),"Restart")){
            // do something
        }
        // Insert the rest of the pause menu logic
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Hey,thanks for your reply your way seems more logical but it also gives me an error : "Assets/Scripts/Pause.js(4,10): BCE0019: 'paused' is not a member of 'Pause'. " Have you got any idea what this means ? \$\endgroup\$
    – Ssiro
    Jun 7, 2014 at 8:33
  • \$\begingroup\$ add private var paused = false; \$\endgroup\$
    – sertsedat
    Nov 7, 2014 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .