- Home /
Pause Menu?
So after watching a video I think I understand how this code should work and setactive does not turn off my pause menu I have the pauseUI selected as the object rigged to the main camera and nothing happens perhaps the tutorial I was watching is outdated or it's because I have more then one scene? My problem isn't with stopping time it's with setting my canvas to false so that I don't have a pause menu in the middle of my screen. This is my first project in unity so I don't really understand much in terms of programming.
So it turns out the weirdest fix I've done was changing my void's to FixedUpdate save then change it back... I don't know what caused it to work or if my void's weren't right before but it's working.
using UnityEngine; using System.Collections; using UnityEngine.UI; public class Pause : MonoBehaviour { public GameObject pauseUI; private bool paused = false; void Start(){ pauseUI.SetActive(paused); } void Update(){
if (Input.GetButtonDown ("Pause")) {
paused = true;
}
if (paused == true) {
pauseUI.SetActive (true);
Time.timeScale = 0.0f;
}
if (paused == false){
pauseUI.SetActive (false);
Time.timeScale = 1;
Debug.Log ("I am Working");
}
} }
you stopped time completely you should set
Time.timeScale = 0.01F; ins$$anonymous$$d of Time.timeScale = 0;
If you wanna work with UI you should use
using UnityEngine.UI;
and then
public Canvas pauseUI;
It will be easier to access the methods/functions, you won't have to add GetComponent() everywhere ^^ If you wanna reduce a little bit your code you could do
pauseUI.SetActive(paused);
which would save you the if statement, but since in this case you have multiple operations in your if, it's okay like that.
And yes, remember to add an 'f' at the end of your floats values ;)
Where are you setting your pasued boolean false? I Couldn't see it in your code.
Answer by Nomibuilder · Apr 18, 2015 at 07:09 AM
Check your Start() Function it should be Like.
Start()
{
paused = false;
}
Instead of
Start()
{
pauseUI.SetActive(paused);
}
Answer by Erathor · Apr 17, 2015 at 07:10 AM
yes and look if u have any other scripts where u use the Time.timeScale
Answer by Starbound Explorer. · Apr 17, 2015 at 10:18 PM
I have a spawning script that uses Time.Deltatime does that count? and sorry about the poor coding I legitimately just copied this off a tutorial I'm going to show an edited version of the code that just throws a bunch of errors from trying to change it to a public canvas it has to be a gameobject for setactive to work