- Home /
Why is my gameObject.Find("Spotlight").GetComponent.() not working?
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".
Just a quick note, in Unity 5, you need to use setEnable(false). Now I haven't been through your whole code, but that could be an issue.
Interesting. Before making the light switch object, I had the toggle set to a key, just in its own script, and it worked. Looks like this:
var on : boolean = false;
function Update(){
if(Input.Get$$anonymous$$eyDown("f")){
on = !on;
}
if(on){
GetComponent.<Light>().enabled = true;
}
if(!on){
GetComponent.<Light>().enabled = false;
}
}
And I just finished trying what you said. I get an error message now that says, "$$anonymous$$issing$$anonymous$$ethodException: UnityEngine.Light.setEnable"
my code snippet looks like this (did I write it incorrectly?):
if (open){
GetComponent.<Animation>().Play(closeAnimationString);
open = false;
alreadyChecked = true;
//Here
myLight.setEnable(false);
if (closeSound){
GetComponent.<AudioSource>().PlayOneShot(closeSound);
}
}
if (!open && !alreadyChecked){
GetComponent.<Animation>().Play(openAnimationString);
open = true;
//and Here
myLight.setEnable(true);
if (openSound){
GetComponent.<AudioSource>().PlayOneShot(openSound);
}
Big thank you for your attention! I appreciate it.
Answer by penguinburger1 · Jun 21, 2015 at 03:25 PM
Got it. I had the lightToggle script (that I commented) still attached, and its default was off, so because that was off, I never could turn it on with the main script.
Again, thank you all for your attention, concern, help, advice, and all that. It means a lot that you'd try to help me out with this!
Answer by iceicetonne · Jun 21, 2015 at 03:25 PM
maybe it works when you change your myLight variable : var myLight : Light = gameObject.Find("Spotlight").GetComponent.();
to something like: public var myLight : GameObject = gameObject.GetComponent.();
in inspector you should assign your light you want to turn on/off
Answer by Maethorion · Jun 21, 2015 at 06:08 PM
If I'm not mistaken, gameObject.Find() only finds gameobjects that are enabled, so if your light is disabled when your script tries to find it, it might be null. Not sure if that's the problem then