Whats wrong with this code?

I cant understand whats wrong with this code,it just wont work.
Any tips?

using UnityEngine;
using System.Collections;

public class PauseMenu0 : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if(Input.GetKeyDown(KeyCode.Escape))
        {   
            GameObject.Find("PauseMenu").gameObject.SetActive(true);
            print ("Paused");
        }   
    }
}

i assume you are getting a null reference when hitting escape?
and i further assume that “PauseMenu” is a UI element within a canvas?

try sth like:

FindObjectOfType<Canvas>().transform.FindChild("PauseMenu").gameObject.SetActive(true);

Yes i getting “NullReferenceException: Object reference not set to an instance of an object”. And i use NGUI to make the pause menu.

Ill try your code to see if it works better.

well it should, because from my experience GameObject.Find does not return disabled GameObjects but they are still a child of their parent

good luck and report back - i am also curious :stuck_out_tongue:

I have done like this before to deactivate objects while the menu is up, cant just remember how i did lol.

And i still getting null reference with the canvas code

the thing is i can perfectly reproduce your problem(and solve it with code above).

for me

void Update()
{
Debug.Log(GameObject.Find("PauseMenu"));
}

returns null if the UI element is disabled.
and “PauseMenu (UnityEngine.GameObject)” if enabled

the code i provided above activates it tho - but then again, i Have no clue about NGUI

NGUI “PauseMenu” are just as other things a object in the Hierarchy that contains the stuffs.
I mean, it should be the same principle as switch between 2 camers, you disable “fps” cam while enable the 3rd person one?

As a general rule you cant use any of the Find functions on disabled GameObjects. A better way is simply to pass in a reference in the inspector.