6

So below you will find a short snippet of code. What this code does is it allows the player to hit the 'p' key to pause the game and when this happens a gui pops up and the players look and movement controls are disabled. My problem is with deactivating and reactivating the gui because it is a game object. It lets me deactivate it but when I try to activate it I get an error.

Code:

    UnityEngine.Component walkScriptOld = GameObject.FindWithTag ("Player").GetComponent ("CharacterMotor");
    UnityEngine.Behaviour walkScript = (UnityEngine.Behaviour)walkScriptOld;
    UnityEngine.GameObject guiMenu = GameObject.FindWithTag ("Canvas");
    if ((Input.GetKey ("p")) && (stoppedMovement == true)) {
        stoppedMovement = false;
        walkScript.enabled = true;
        guiMenu.SetActive(true);
    } else if ((Input.GetKey ("p")) && (stoppedMovement == false)) {
        stoppedMovement = true;
        walkScript.enabled = false;
        guiMenu.SetActive(false);
    }

Error:

NullReferenceException: Object reference not set to an instance of an object MouseLook.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs:44)
Jehof
  • 34,674
  • 10
  • 123
  • 155
Jonah Starling
  • 539
  • 6
  • 20
  • It looks like deactivation is destroying the instance `guiMenu`. You should check if that is what happens when you set the property `SetActive` to false. – Verhaeren Dec 15 '14 at 03:49
  • @Verhaeren I looked it up and all it said is that when you turn a gameobject off it turns all of that objects scripts off and the update will no longer be called. It says you can activate and deactivate game objects but how? – Jonah Starling Dec 15 '14 at 03:58
  • The best I can do right now is upvoting your question so it gets more visible to other peple willing to help. Right now I have no clue. – Verhaeren Dec 15 '14 at 04:02
  • What I just did instead was turned off a component of the canvas that controlled whether or not the gui was visible – Jonah Starling Dec 15 '14 at 04:07
  • Glad that has worked for you! – Verhaeren Dec 15 '14 at 04:15

1 Answers1

1

It seems that the code you've given here is in an Update. So the guiMenu object is being found and stored every frame.

What you want to do is cache the object in the Awake or Start function, and the rest of the code will work just fine. Also note that caching is always good practice.

    //This is the Awake () function, part of the Monobehaviour class
    //You can put this in Start () also
    UnityEngine.GameObject guiMenu;  
    void Awake () {
        guiMenu = GameObject.FindWithTag ("Canvas");
    }

    // Same as your code
    void Update () {
        if ((Input.GetKey ("p")) && (stoppedMovement == true)) {
            stoppedMovement = false;
            walkScript.enabled = true;
            guiMenu.SetActive(true);
        } else if ((Input.GetKey ("p")) && (stoppedMovement == false)) {
            stoppedMovement = true;
            walkScript.enabled = false;
            guiMenu.SetActive(false);
        }
    }
Programmer
  • 121,791
  • 22
  • 236
  • 328
Venkat at Axiom Studios
  • 2,456
  • 1
  • 15
  • 25