The script is for a light switch that when triggered with “e”, is supposed to turn on a spotlight in the scene. It’s not turning on though.
I disabled the light in the scene so I could turn it on with my lightswitch in game. However, even though I get no Error message, it just isn’t enabling.
Any suggestions?
EDIT
I’ve just found out that when I’m in game, I can’t enable the Light checkbox, even in the inspector… so the script isn’t letting me enable it.
SpotlightSwitch script
var open : boolean = false;
var openAnimationString : String;
var closeAnimationString : String;
var buttonTransform : Transform;
var distToOpen : float = 2;
@HideInInspector
var playerTransform : Transform;
@HideInInspector
var cameraTransform : Transform;
var openSound : AudioClip;
var closeSound : AudioClip;
function Awake () {
playerTransform = GameObject.FindWithTag("Player").transform;
cameraTransform = GameObject.FindWithTag("MainCamera").transform;
if(open){
GetComponent.<Animation>().Play(openAnimationString);
}
}
function Update () {
//my getComponent variable.
var myLight : Light = gameObject.Find("Spotlight").GetComponent.<Light>();
var alreadyChecked : boolean = false;
var angle : float = Vector3.Angle(buttonTransform.position - cameraTransform.position,
buttonTransform.position + (cameraTransform.right * buttonTransform.localScale.magnitude) -
cameraTransform.position);
if (Vector3.Distance(buttonTransform.position, playerTransform.position) <= distToOpen){
if (Vector3.Angle(buttonTransform.position - playerTransform.position, cameraTransform.forward) < angle){
if (Input.GetKeyDown("e") && !GetComponent.<Animation>().isPlaying){
//Here is my enable/disabling below.
if (open){
GetComponent.<Animation>().Play(closeAnimationString);
open = false;
alreadyChecked = true;
myLight.enabled = false;
if (closeSound){
GetComponent.<AudioSource>().PlayOneShot(closeSound);
}
}
if (!open && !alreadyChecked){
GetComponent.<Animation>().Play(openAnimationString);
open = true;
myLight.enabled = true;
if (openSound){
GetComponent.<AudioSource>().PlayOneShot(openSound);
}
}
//First attempt.
/*if (open){
myLight.enabled = true;
}
if (!open){
myLight.enabled = false;
}*/
}
}
}
}
(This script is the same script I’m using for my door, I just got lazy and didn’t change the script to say “hit” or something instead of “open” and “close”.