- Home /
How to turn on/off multiple lights using GUI buttons?
Hi everyone,
I'm making a sample test on switching an on/off multiple lights using 1 GUI button. Right now I'm using javascript because I'm still learning unity. Could anybody give me some sample codes that I' could use. Thanks.
You need to describe your setup, are all the lights children of any thing? Are they prefabs, are they named, are they attached to the camera? Are you holding references to them in the script that is meant to disable(turn off/on) them?
Hi Sir,
In my scene, I have 1 main camera, 2 point lights, and a plane. The lights are name Point Light 1 and Point Light 2, and the script is attached to the point lights which will be the one to control them at the same time. I want to use a gui button so that when I click the gui it will turn on/off those two lights.
Answer by Berenger · Feb 07, 2013 at 02:47 PM
That should do it.
var lights : Light[];
function OnGUI()
{
var on = lights[0].enabled;
if( GUI.Button( Rect( 10, 10, 100, 100 ), "Turn " + (on ? "off" : "on") + " the lights !" ) )
{
for( var i = 0; i < lights.Length; i++ )
lights[i].enabled = !on;
}
}
Hi sir,
When I try to put the codes on to my unity, this errors showed up:
Assets/Scripts/Control_Lights.js(7,39): BCE0043: Unexpected token: 100.
Assets/Scripts/Control_Lights.js(7,94): BCE0043: Unexpected token: ).
Assets/Scripts/Control_Lights.js(9,9): BCE0043: Unexpected token: for.
Assets/Scripts/Control_Lights.js(9,12): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/Control_Lights.js(9,14): BCE0043: Unexpected token: var.
Assets/Scripts/Control_Lights.js(9,18): BCE0044: expecting ), found 'i'.
Assets/Scripts/Control_Lights.js(9,48): BCE0043: Unexpected token: ).
Assets/Scripts/Control_Lights.js(13,1): BCE0044: expecting EOF, found '}'.
Is there anything that I do wrong? But anyways, thanks for helping me on my script.
Well, coding on the fly is risky business. I should have tested it first Anyway, I forgot a some parenthesis in the if line. I fixed the code.
Your answer
