Light switch trigger

I am having a bit of difficulties with the switch light sensor(although it’s more of a button sensor).I’ve made this simple code and it doesn’t work.

var switchLight : Transform;

function Update () {

	if(Input.GetKey("q"))
	{
	
		switchLight.enabled = true;
	
	}
	else if(Input.GetKey("t"))
	{
		switchLight.enabled = false;
	} 

It gets an error : Field “UnityEngine.Transform.enabled” not found.

Hasn’t it been declared as a boolean? Is that the mistake?

3 Answers

3

You can’t enable or disable the transform. If it’s a point light or another kind of light you should do :

var light : Transform;

 light.intensity =0; // set the intensity to 0 , which will give no light.

Or if it’s just a value that you wan’t to make false or true, yes you should use boolean.

var light : boolean = false;

very simple fix :stuck_out_tongue:

var switchLight : GameObject;

OR

var switchLight : Light;

because u want to change the value of a GameObject’s component not the transform component(transform is only for changing position, rotation or scale), and to make only one button

function Update()
{
    if (Input.GetKeyDown(KeyCode.F))
    {
    	light.enabled = !light.enabled;
    }
}

New 2019 Tutorial