My light Switch works when turning off the light, but when turning it on it doesn’t work, it makes a quick flash of the light and that purple color that means that i’m taking the material out, but only like for a second. I guess my script is doing the turn On/Off function only in one way. (I need that this script affects the light, the Halo, and the Shader when “e” is pressed)
The Script:
var lightSource : Light;
var Halo : Behaviour;
var shootSound:AudioClip;
static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
private var inTrigger = false;
function OnTriggerEnter(other: Collider){
if (other.CompareTag("Player")) inTrigger = true;
}
function OnTriggerExit(other: Collider){
if (other.CompareTag("Player")) inTrigger = false;
}
function Update ()
{
if (inTrigger Input.GetKeyDown("e")) TurnOnLights();
}
function TurnOnLights()
{
lightSource.enabled = !lightSource.enabled;
Halo.enabled = !Halo.enabled;
renderer.material.shader = Shader.Find ("Self-Illimin/Diffuse");
yield;
lightSource.enabled = false;
Halo.enabled = false;
renderer.material.shader = Shader.Find ("Transparent/Diffuse");
}
What did you intend to do after the yield line? Looks like that it will actually turn off because you told it to turn off after the yield.
Maybe it’s turning off inmediatly after the previous action, That could be the reason it only makes flashes on “e”… Could you help me toggling between On and Off by pressing “e” key?? (or clicking if you know the way, i like that better:))
I’ll try because I always write my code in C#.
The way I see it is that the user can toggle the lights if he is inside the Trigger.
var lightSource : Light;
var Halo : Behaviour;
var shootSound:AudioClip;
static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
private var inTrigger : boolean = false;
function Awake() //use this to initialize your light's state and other variables
{
lightSource.enabled = false;
Halo.enabled = false; //etc..
}
function OnTriggerEnter(other: Collider){
if (other.CompareTag("Player"))
inTrigger = true;
}
function OnTriggerExit(other: Collider)
{
if (other.CompareTag("Player"))
inTrigger = false;
}
function Update ()
{
if (inTrigger Input.GetKeyDown("e"))
ToggleLights(); //this name fits more than TurnOnLights()
}
function ToggleLights()
{
if(turnedOn) //If he presses 'e' and the lights is on, switch it off
{
lightSource.enabled = false;
//turn off properties here...
turnedOn = false; //now the light is turned off
}
else // if it's turned off, turn it on
{
lightSource.enabled = true;
// turn on properties here...
turnedOn = true; //now the light is turned on
}
}
I’d do something just a bit different, but same effect in the end as Pheleno - the only reason i’d take this approach over the toggle approach is that it allows for directly setting the lights to a state (i.e. SetLightsOn( true ); ) from outside code - you could however treat turnedOn as a property and override the setter (using C# anyhow) to handle this cleanly as well.
function Update ()
{
// check if we are within a light trigger zone, allow for light trigger zone specific actions if so
if( inTrigger )
{
// user pressed 'e' within the light trigger zone
if( Input.GetKeyDown( "e" ) )
{
// toggle the lights state based on current state of lights
SetLightsOn( !turnedOn );
}
}
}
function SetLightsOn( bool lightsOn )
{
// set the current light state
turnedOn = lightsOn;
// set the light source based on current light state
lightSource.enabled = turnedOn;
// set the halo based on whether or not lights are on
Halo.enabled = turnedOn;
// take appropriate action based on light state
if( turnedOn )
{
renderer.material.shader = Shader.Find ("Self-Illimin/Diffuse");
}
else
{
renderer.material.shader = Shader.Find ("Transparent/Diffuse");
}
}
Ew, TurnLightsOn(false) is pretty bad
You also do some unnecessary assignments.
if (Input.GetKeyDown(KeyCode.E))
ToggleLight();
void ToggleLight()
{
turnedOn = !turnedOn;
lightSource.enabled = turnedOn;
// etc
}