The question is answered, right answer was accepted
Pause Menu Problems
Hello, I am having a problem with my C# pause script. What happens is when "escape" is pressed to open the menu it's fine. On the other hand when you press it to close the menu it won't open anymore. I can see that it turns the script off when you close the menu but it cannot turn it back on for some reason. Can anyone help me? thanks in advanced. Here's my script:
using UnityEngine; using System.Collections;
public class PauseGame : MonoBehaviour { public Transform Canvas; public Transform Player;
//Update is called once per frame
void Update () {
if (Input.GetKeyDown("escape"))
{
Pause();
}
}
public void Pause()
{
if (Canvas.gameObject.activeInHierarchy == false)
{
Canvas.gameObject.SetActive (true);
Time.timeScale = 0;
GameObject Player = GameObject.FindGameObjectWithTag ("Player"); enabled = true;
}
else
{
Canvas.gameObject.SetActive(false);
Time.timeScale = 1;
GameObject Player = GameObject.FindGameObjectWithTag ("Player"); enabled = false;
}
}
}
Answer by Hatsuko · Nov 08, 2017 at 10:37 AM
@NEGATIVERAGDOLL when things like this happens, you might want to test "exactly which line is reached in the code" (sorry for poor English). You can do this by putting Debug.Log()
whithin code and check Console to see if it gets printed out or not, or by putting breakpoints if you know how to use the debugger, which comes with the built-in MonoDevelop or other script editor.
You can put Debug.Log("!")
in Update()
to test. I believe you will find that after you press escape, no more "!" gets printed. This is because when a Component is disabled, its Update()
is not being called. Also when a GameObject is deactivated, all attached Components' Update()
are not being called. There are several built-in event functions that are only called when being enabled/active. Update()
is one of them. I currently can't find a list though... but you can always test it yourself when needed :)
In your script there are several lines (that might be) related to this issue.
Canvas.gameObject.SetActive(false); // This is not related if your script is not attached to the Canvas itself
and
enabled = false;
You might want to think about how you want to design your Pause Menu.
Hey, thank you for the comment. Is there anyway you could show me what you mean? I don't fully understand how I am supposed to fix my problem. Thank you for taking your time to reply :)
This is the Hierarchy window. Everything here is called a GameObject. This is the Inspector window. Everything here (well, mostly) is called a Component. In this case your PauseGame script is a Component.
When the 1 check box is turned off, we say the GameObject is not active / deactivted.
When the 2 check box is turned off, we say the Component is not enabled / disabled.
These are just terms used by Unity. As long as we understand each other, it's okay to say whatever we want.
I can see that it turns the script off when you close the menu but it cannot turn it back on for some reason.
When your script is turned off (=deactivted), its Update() will not be called. It means the code inside Update() will not be executed. It means the below code won't run:
if (Input.Get$$anonymous$$eyDown("escape"))
{
Pause();
}
That's why "it cannot turn it back"... because it's turned off!
So remove your enabled = false;
code. You cannot turn off a script and expect its Update() still running.
Ps. In the answer, what I mentioned in the first paragraph is this: https://www.youtube.com/watch?v=$$anonymous$$h8Y0LW-jRA. The Debug.Log() thing is in Tip #5. $$anonymous$$ight be helpful :)
@Hatsuko Thank you so much. All I had to do was remove the enabled = false; as you said!! :)
Follow this Question
Related Questions
C# - JS problem. 1 Answer
Enabling & Disabling Script via On Click 1 Answer
Health Script 2 Answers
i need help jump script c# 1 Answer