0
votes

Can someone fix my pause menu script ?(I want it to stop the ball) Here's the link of my project: http://mediafire.com/download/33poh7jalkcvjzh/Jumpy+ball.rar

using UnityEngine; using System.Collections;

public class pauseMenu : MonoBehaviour { public GUISkin myskin;

private Rect windowRect;
private bool paused = false , waited = true;
private float time = 60f;

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);
    }
    if(!paused)
        if(time>0)
            time -= Time.deltaTime;
}

private void OnGUI()
{
    if (paused)
        windowRect = GUI.Window(0, windowRect, windowFunc, "Pause Menu");
    GUI.Box(new Rect(1, 2, 70, 30), "Time: " + time.ToString("0"));
}

private void windowFunc(int id)
{
    if (GUILayout.Button("Resume"))
    {
        paused = false;
    }
    if (GUILayout.Button ("Restart")) 
    {
        Application.LoadLevel("LEVEL 1");
    }
    if (GUILayout.Button("Quit"))
    {
        Application.LoadLevel("Main Menu");
    }
}

}

2
you've posted the same question 3 times and in fact on a previous post you already have an accepted answer :) You might get more/quicker help on the unity forums.uchamp
I know but you gave me a good ideaGabi Dobre

2 Answers

1
votes

Use Time.timeScale to pause/unpause the game (i.e. stop the ball from moving), like this:

Time.timeScale = 0f; // paused
Time.timeScale = 1f; // unpaused

Bear in mind that Time.deltaTime will then be 0 when Time.timeScale is 0, so you might want to use Time.unscaledDeltaTime in some cases.

0
votes

Use this instead:

IEnumerator StopBall(float delay)
{
    yield return new WaitForSeconds(delay);
    // Stop the ball code
}

Call it like this:

StopBall(0.5f);

Where 0.5f is the amount you want to wait. Remember to add F to the end of the number since it's a floating point. Regards.