How to activate and deactivate a GameObject?


I was following the tutorial on how to activate GameObjects at but when I try to compare the active state with activeSelf or activeInHierarchy, I receive the error: Object reference not set to an instance of an object…

public class KeyboardInterfaceHelper : MonoBehaviour {

	public bool currentState;
	private GameObject interfaceHelper;

	void Update () 
		if (Input.GetKey (KeyCode.K)) 
			Debug.Log("You entered the K key...");

			currentState = interfaceHelper.activeInHierarchy;

			if (currentState == true) {
				// The GameObject was active, so we deactivate it...
				interfaceHelper.SetActive (false);
				// The GameObject was inactive, so we activate it...
				interfaceHelper.SetActive (true);

The idea is to toggle the GameObject between active and inactive whenever the player press certain key. So, if the GameObject is active it should become inactive and viceversa.

Any ideas why this code does not work?

Thank you.

I would think interfaceHelper was not set in inspector window, from looking at the pieces of code.
Make interfaceHelper public, then drag the object you want to check to “interfaceHelper” in inspector (usually right-docked window).

You didn’t assign anything to the interfaceHelper gameobject. It’s empty.