one button to control 2 lights via javascript

hello there,

my coding skills are not sufficient for this presumably easy task. i searched through the forums and couldnt find what i am looking for, so i need your help. here is the question.

i have 2 lights in the scene and a button to control them to make a very simple day/night cycle-loop. 1 directional light and a spot light. at the begining i need the directional light to be active(on) and the spot light to be inactive(off). and when i hit the button i need it to change it otherwise: turn directional light off and turn spot light on…

is there a way out there to do this? i really appreciate for the help…

I could just write down the code for you, but I’m doing you a much bigger favor by giving you this link.

http://walkerboystudio.com/html/unity_training___free__.html

If you prefer reading, check this one:

http://www.unityscript.com/lessons1/basics.php

wuhuu :slight_smile: great resource thank you… however it would take me forever to dig out and understand how to do i am looking for. but most deffinetely i will look into this training. in the mean time, would you please be so kind to help me with the code?

thank you again and again

I’m not going to write the entire script for you but this should get you started to figure out how things work. Attach this script to an (empty) game object, and drag a directional light/whatever light into the light1 variable slot. If you press play and then you press the Return (Enter) key the light gets enabled/disabled.

The first script is in javascript (some people prefer to call it unityscript). The second script is C#, but they both do the same. Try to figure out the differences and similarities.

If you don’t know what to do, I recommend to take a look at the links I’ve provided you with.

Good luck :slight_smile:

	public var light1 : GameObject;	
	
	// Update is called once per frame
	function Update () 
	{	
		if (Input.GetKeyDown(KeyCode.Return)  light1.light.enabled == true)
		{
			light1.light.enabled = false;
		}
		
		else if(Input.GetKeyDown(KeyCode.Return)  light1.light.enabled == false)
		{
			light1.light.enabled = true;
		}
	}
using UnityEngine;
using System.Collections;

public class LightControl : MonoBehaviour 
{
	
	public GameObject light1;	
	
	// Update is called once per frame
	void Update () 
	{	
		if (Input.GetKeyDown(KeyCode.Return)  light1.light.enabled == true)
		{
			light1.light.enabled = false;
		}
		
		else if(Input.GetKeyDown(KeyCode.Return)  light1.light.enabled == false)
		{
			light1.light.enabled = true;
		}
	}
}

good news i got it:

private var SunOn: boolean = true;
private var SunOn2: boolean = true;
private var int1: boolean = false;
private var int2: boolean = false;
private var int3: boolean = false;

var SunLight : Light;
var SunLight2 : Light;
var intLight1 : Light;
var intLight2 : Light;
var intLight3 : Light;

function OnGUI()
{
if (GUI.Button(Rect(100,70,100,100),“Click”))
{
SunOn = !SunOn;
SunLight.enabled = SunOn;
SunOn2 = !SunOn2;
SunLight2.enabled = SunOn2;

int1 = !int1;
intLight1.enabled = int1;
int2 = !int2;
intLight2.enabled = int2;
int3 = !int3;
intLight3.enabled = int3;
}
}

the only thing is to turn the lights off at the inspector first :slight_smile:

You can turn them off in the start function. For example:

function Start()
{
SunLight.enabled = false;
}

This way the light is turned off when you press play.

Also I see you write this:

int1 = !int1;
intLight1.enabled = int1;

You could do this instead, it’s the same thing but easier to see what’s going on.

int1 = false;
intLight1.enabled = false

On a more elegant way to do it with less lines of code:

function ToggleLights()
{
	light1.enabled = !light1.enabled;
	light2.enabled = !light2.enabled;
	light3.enabled = !light3.enabled;
}

Using the ! in front of a boolean is the same as using a - in front of a number. It gives you the inverse value. so !true means false and !false means true. O-o at least I’m fairly certain that works in unity. I think I’ve done it before.

Also, I would suggest having an array and use a foreach loop. So it’d be something like this:

var lights:GameObject[]; //Dynamic array in unity javascript

function ToggleLights()
{
	for(currentLight:GameObject in lights)
	{
		currentLight.light.enabled = !currentLight.light.enabled; //need to get the light component of the gameObject.
	}
}

Simply call the function any time you want to toggle them. This just toggles though, so you’ll have to have all lights on that you want to turn off, and all lights off that you want to turn on. But I am guessing you already have it set up that way. All you have to do is attach the script to something in the scene, and drag all the lights you want to effect to the script’s array. Simple as that. :slight_smile:

**Note, didn’t test any of this as I’m at work and can’t, but it “should” work.

if you want to use a light switch; and the light is across the room, add a box collider to light switch or object you want to turn on/off the lights and mark it trigger.

then add this to it, this is JavaScript:

var linkedLight : Light;

function OnTriggerStay()
{
if(Input.GetKeyDown(“e”))
{
linkedLight.enabled = !linkedLight.enabled;
}
}

function LightOff (){
linkedLight.enabled = false;
}

then add the light you want to turn on and off in the inspector. “it’s a little slow”

i don’t know if this is what you had in mind, but it was worth a shot.