So i am a bloddy beginner with unity and i am supposed to use get componment to “find” a spotlight and then make it toggle by pressing a button, lets take the L button for now. I have no idea how to do it though… i watched tutorials and read some forum posts but nothing involving get component… and i dont really get it either… i need a javascript file. Can anyone help me out?
Thanks Guys.
(also sorry for grammer or spelling mistakes, i am german XD)
You don’t need to use GetComponent() to toggle a light, but you do need an instance of the light.
The code below finds a spotlight called “spotlight01” and toggles it using the L key.
(I’m sorry if I’ve dumbed it down too much.)
// This variable is a reference to the spotlight we find in Start().
private var lightToToggle : GameObject;
function Start () {
// light is found here. This only needs to be called once, but will only find this specific light.
lightToToggle = GameObject.Find("spotlight01");
}
function Update () {
// Checks if the L key has been pressed.
if (Input.GetKeyDown(KeyCode.L)){
// Sets the value for the light's 'enabled' variable to its opposite.
// If true, it will be set to false.
// If false, it will be set to true.
lightToToggle.light.enabled = !lightToToggle.light.enabled;
}
}