- Home /
How can I insert Load Level in this Script?
How can I insert LoadLevel in the ''Option'' button at the end?
using UnityEngine;
using System.Collections;
public class pauseMenu : MonoBehaviour
{
public GUISkin myskin;
private Rect windowRect;
private bool paused = false , waited = true;
private void Start()
{
windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200);
}
private void waiting()
{
waited = true;
}
private void Update()
{
if (waited)
if (Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.P))
{
if (paused)
paused = false;
else
paused = true;
waited = false;
Invoke("waiting",0.3f);
}
}
private void OnGUI()
{
if (paused)
windowRect = GUI.Window(0, windowRect, windowFunc, "Pause Menu");
}
private void windowFunc(int id)
{
if (GUILayout.Button("Resume"))
{
paused = false;
}
if (GUILayout.Button("Options"))
{
}
if (GUILayout.Button("Quit"))
{
}
}
}
Answer by Gruffy · Apr 04, 2014 at 10:39 PM
Hey bud.
here at your Option menu button you should write...
if (GUILayout.Button("Options"))
{
Application.LoadLevel(0); //this could be a string
//representing your level name or a number representing its
//place in your scene build hierarchy
}
Edit:
Thanks for this dude, but do you know how to stop de scene when I press escape? because when I press escape the pause menu appear but the scene do not stop
This is another question really matey but since you have not marked this as answered, this once I will addend the answer, but in the future, you should understand you have asked a single question and that has been answered for you. Your next question should be a new one or use the site as was intended and search , you would have most definitely found numerous answers regarding this and also the Unify Community pages are riddled with examples pertaining to your questions, both original and addended. Anyway, to allow the game to pause as you wish, you should add a method/call that can change Time.time
So... firstly a new boolean to hold a true or false value, this helps us determine if a button has been pressed or not.
using UnityEngine;
using System.Collections;
public class PauseGame : MonoBehaviour {
private bool isTrue = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyUp(KeyCode.Escape))
{
if (isTrue)
{
isTrue = false;
Time.timeScale = 1.0f;
Debug.Log("The Game is now Rolling again " + Time.timeScale.ToString());
}
else
{
isTrue = true;
Time.timeScale = 0.0f;
Debug.Log("The Game is now paused " + Time.timeScale.ToString());
}
}
}
}
The above is set to trigger when you lift your finger up from the Escape key or back button found on mobile devices.
That should solve your problem with loading a level (& NOW PAUSING WHEN PRESSING "ESCAPE" KEY)on pressing the options button. Take care bud and if that has solved both your questions, then please mark it as an answer as it will help those who do use the search field on this site to obtain a faster result with any similar problems too. Cheers bud Gruffy
Thanks for this dude, but do you know how to stop de scene when I press escape? because when I press escape the pause menu apear but the scene do not stop
Thanks for this dude, but do you know how to stop de scene when I press escape? because when I press escape the pause menu apear but the scene do not stop
when you press escape, use Time.timeScale = 0, then the scene will be pause: https://docs.unity3d.com/Documentation/ScriptReference/Time-timeScale.html