Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Pause Menu with restart not to be working as well.

Discussion in 'Scripting' started by JACKO4590, Dec 1, 2014.

  1. JACKO4590

    JACKO4590

    Joined:
    Nov 25, 2014
    Posts:
    81
    Ok well i got this basic script and modified it abit but i have a problem, every time i hit the pause menu (ESC) it auto restarts and the other error is it cant find level 3 as well, so its trying to load all the Applications and not the one i want to click on.

    can anyone help me

    i want it so when i click the GUI button it then restarts (and the same for quit and so on)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PauseMenu : MonoBehaviour {
    5.    
    6.     private int buttonWidth = 200;
    7.     private int buttonHeight = 50;
    8.     private int groupWidth = 200;
    9.     private int groupHeight = 170;
    10.     bool paused = false;
    11.  
    12.     void Start()
    13.     {
    14.         Screen.lockCursor = true;
    15.         Time.timeScale = 1;
    16.     }
    17.    
    18.     void OnGUI ()
    19.     {
    20.         if(paused)
    21.             {
    22.             GUI.BeginGroup(new Rect(((Screen.width/2) - (groupWidth/2)),((Screen.height/2)), groupWidth, groupHeight));
    23.             if(GUI.Button(new Rect(0,0,buttonWidth,buttonHeight),"Main Menu"));
    24.             {
    25.                 Application.LoadLevel (3);
    26.             }
    27.             if(GUI.Button(new Rect(0,60,buttonWidth,buttonHeight),"Restart Game"));
    28.             {
    29.                 Application.LoadLevel ("Level03");
    30.             }
    31.             if(GUI.Button(new Rect(0,120,buttonWidth,buttonHeight),"Quit Game"));
    32.             {
    33.                 Application.Quit();
    34.             }
    35.             GUI.EndGroup();
    36.         }
    37.     }
    38.            
    39.     void Update ()
    40.     {
    41.         if(Input.GetKeyDown(KeyCode.Escape))
    42.             paused = togglePause();
    43.     }
    44.            
    45.     bool togglePause()
    46.     {
    47.         if(Time.timeScale == 0)
    48.         {
    49.             GameObject.Find("First Person Controller").GetComponent<MouseLook>().enabled = true;
    50.             Screen.lockCursor = true;
    51.             Time.timeScale = 1;
    52.             return(false);
    53.         }
    54.         else
    55.         {
    56.             GameObject.Find("First Person Controller").GetComponent<MouseLook>().enabled = false;
    57.             Screen.lockCursor = false;
    58.             Time.timeScale = 0;
    59.             return(true);
    60.         }
    61.     }
    62. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    You dont what the semicolons ";" on the end of the three if conditions. They are the "end of instruction" indicators and you want the subsequent scoped instructions to be controlled by those conditions.

    With those semicolons in place as they are your OnGUI function reads as
    If a button is pressed do nothing
    Load the third scene in the build
    .... The rest is irrelevant since the scene was just loaded...
     
    JACKO4590 likes this.
  3. JACKO4590

    JACKO4590

    Joined:
    Nov 25, 2014
    Posts:
    81
    Ah yes, thank you. im still getting my head around this and this is abit more advance then what im use to, this fixed it and now i can continue. thank you for your help