How Do I Enable a GameObject upon Button Press?

I want a green spotlight thing to enable when I press ‘tab’. Here is the code I have so far. This script is on the green spotlight itself.

using UnityEngine;
using System.Collections;

public class SelectPlayer : MonoBehaviour 
{

	// Use this for initialization
	void Start () 
	{
		if(Input.GetKey(KeyCode.Tab))
		{
			gameObject.SetActive(true);
		}

	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
}

Nothing happens when I press tab. I’ve tried setting the GameObject on and off manually in-client before testing, and either way it doesn’t work. Thanks in advance!

You can’t have a GameObject set itself to active. This is because Unity does not do any call backs on inactive GameObjects.

Put this script on another GameObject that is active in the scene. Pass it a referece in the inspector to you inactive object, and it will work.

You must process input in Update, start is called only once.

void Update () {
    if(Input.GetKeyDown(KeyCode.Tab)){
	    gameObject.SetActive(!gameObject.activeSelf);
	    Debug.Log("set");
    }
    Debug.Log ("Update is called");
}

This will set gameobject active state to opposite. Notice, that you will be able only
to disable the gameobject, as disabled components aren’t updated, “Update is called” wont be called. So in this case there’s really no way of re-enabling the gameobject from same gameobject.

The solution is to process SetActive() from other gameobject with active state is always true.
It can either be parent or completely separate gameobject.