- Home /
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?
Answer by Inan-Evin · Oct 13, 2011 at 03:17 PM
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;
Yes, thank you I fixed it as soon as I posted it, wanted to delete it but you answered my second question that I wanted to ask, how to change the light intensity. Thanks!
Answer by leonalchemist · Oct 13, 2011 at 04:03 PM
very simple fix :P
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;
}
}
Yes, figured it out, though a nice explanation for the transform component, as funny as it sounds, didn't know that.
Answer by markmozza · Jan 21, 2019 at 11:56 AM
New 2019 Tutorial https://www.youtube.com/watch?v=kqu-lsfd4qI&t=1s