- Home /
A simple light switch
Hi I am very new to scripting
So Please treat me like an idiot. I won't mind.
I just want to make a light switch on a box on a wall that when clicked with a mouse toggles of and on a light globe on a ceiling.
I have managed to figure out how to mouse click and toggle variables. But how do I send the on off command to the light script?
For that matter how do I run functions in other scripts/objects from a different object?
I have spent hours and hours searching the references and help and forum and the only answers I found may as well have been written in zulu.
WHAT I'VE DONE SO FAR.............
//Add a toggle variable for light on off call it lightState static var lightState : boolean;
function Update () {
// Make sure the user pressed the mouse down if (!Input.GetMouseButtonDown (0)) return;
var hit : RaycastHit;
if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), hit, 100))
{
lightState = (!lightState);
print ("var lightstate = " + lightState);
}
else {
print ("I've clicked nothing!");
}
}
Thanks
Daivd
Also, is there a good book on javasript relevant to unity that I should get?
Answer by duck · Mar 08, 2010 at 10:43 AM
While raycasting is a very valuable thing to learn about when using unity, you could implement clickable light switches in a much simpler way. Place this script on each lightswitch object:
var linkedLight : Light;
function OnMouseDown() { linkedLight.enabled = !linkedLight.enabled; }
That is it. Now just drag that script onto each lightswitch. Once you have that script on each lightswitch, you need to drag a reference to the corresponding light into the "light"
variable for each switch. This creates a reference link to each switch's light.
Note that you can have more than one switch per light using this method.
For more information about how to communicate between scritps and other objects, see this answer:
Thanks that was a beautiful solution. You are a Legend. That was sooooo easy. I had read about dragging, but didn't understand until I followed your instructions.
Thanks a million.
"Once you have that script on each lightswitch, you need to drag a reference to the corresponding light into the "light" variable for each switch."
Can you please explain what this means for a total noob?
Answer by Jito Jorge · Aug 04, 2012 at 12:47 PM
Hello !
I have another question. I try to invert the code because I want to turn off one light, and when this light is off, turn too another light on.
BUT .enabled doesn't exist and I try to invert the code like this
var linkedLight : Light;
function OnMouseDown() {
!linkedLight.enabled = linkedLight.enabled;
}
but is not good.
Do you have any solution ?
ask questions as new, don't ask in already answered or closed.
Answer by m-bot · Dec 02, 2012 at 09:55 AM
try this for two lights:
#pragma strict
var linkedLight1 : Light;
var linkedLight2 : Light;
function OnMouseDown() {
linkedLight1.enabled = !linkedLight1.enabled;
linkedLight2.enabled = !linkedLight2.enabled;
}
It work! I tested it.
Your answer
Follow this Question
Related Questions
lighting click help 3 Answers
Adding lights and cameras to scenes with cSharp script 2 Answers
How do I initialize a simple light in C#? 2 Answers