Can't make SpotLight work

When I start the simulation, the SpotLight is disabled. I want to enable\disable it by pressing on F.

But my code doesn’t work. I can’t see anything in the console.

GameObject spotLight;
bool isSpotLightActive = false;

void Start ()
{
	spotLight = (GameObject)this.gameObject;
}

void Update ()
{
	if (Input.GetKeyDown (KeyCode.F))
	{
		if(isSpotLightActive == false)
		{
			Debug.Log("acitvate"); // Don't see
			spotLight.SetActive(true);
			isSpotLightActive = true;
		}
		else if(isSpotLightActive == true)
		{
			Debug.Log("deacitvate");  // Don't see
			spotLigh.SetActive(false);
			isSpotLightActive = false;
		}
	}
}
  1. change your spotlight variable to Light, not GameObject.
  2. Use GetKeyUp, because if your using this in a update loop it will just keep cycling the if conditions as it checks roughly 60 times a second.
  3. Use spotlight.enabeled = true/false to activate your light.

Hope this helps!

First you have to get the light component rather than enabling and disabling the whole GameObject.

public class SpotlightScript : MonoBehaviour {

	private Light spotlight;

	void Start () {
		spotlight = GetComponent<Light>();
        spotlight.enabled = false;
	}
	
	void Update () {
		if(Input.GetKeyDown(KeyCode.F)) {
			if(spotlight.enabled) {
				spotlight.enabled = false;
			} else {
				spotlight.enabled = true;
			}
		}
	}
}