It just doesn’t work and does nothing (nothing shows up in console)… It does hide the canvas at start but nothing else does work
public class PauseMenu : MonoBehaviour {
GameObject pauseMenu;
void Start () {
pauseMenu = this.gameObject;
pauseMenu.SetActive (false);
}
void Update () {
if (Input.GetKeyDown (KeyCode.Escape)) {
print ("Escape");
pauseMenu.SetActive (!pauseMenu.activeSelf);
GameObject.Find ("Player").GetComponent().enabled = pauseMenu.activeSelf;
}
}
}
Thanks allot!
Scripts don’t run on an object when you set it to inactive. So once Start
runs and you deactivate the object, Update
never runs.
So you’ll want to put this script on a different object and make your pauseMenu
variable public, and drag the pause menu canvas in the editor to set pauseMenu, and remove pauseMenu = this.gameObject
since it’ll be set in the inspector.
Things should work as expected then.
What script is this on? Ensure you’re not putting a script on an inactive gameobject. You cannot “find” an inactive gameobject.
You can make a reference to it, and plug it in manually in the spector. That’ll run the set active stuff for you, but if it’s off and is later searched for, it won’t come up.
var theObject : GameObject;
Then in the inspector drag the object you want to turn on and off into that slot. Ensure the gameobject with the script you shared is on an object that is never turned off in the scene. I can see that you are actually turning it off in the same script (in start no less). That’s why update isn’t doing anything. Run this script on a gameObject that never turns off, and reference the object you drag into the inspector’s gameobject variable slot to turn it on and off with setactive.
check the project settings in the input corner.
edit > project settings> input
there is a “jump” thing that has “space” key set there.
I’ve got the same problem and turn this key to another one, that worked for me.