Hi, I’m getting this error: “NullReferenceException: Object reference not set to an instance of an object” on line 15 in my script, but I can’t see what’s the problem with the code. I’m trying to check if the var is true or false and change their values when pressing the letter L.
var myCheck : boolean = true;
function Update () {
if(myCheck == true){
var myLight : Light = gameObject.Find("light_test").GetComponent(Light);
myLight.color = Color.green;
}
else
{
myLight.color = Color.red;
}
if(Input.GetKeyUp(KeyCode.L) && myCheck == true){
myCheck = false;
}
else if(Input.GetKeyUp(KeyCode.L) && myCheck == false){
myCheck = true;
}
}
var myLight:GameObject;
drag your light on top of the new slot after saving the script
Thanks for the answers, but I found a way to make it work (maybe it’s not the best way), here’s the code: `#pragma strict
var myCheck : boolean = true;
var trueColor : Color = Color.green;
var falseColor : Color = Color.red;
function Update () {
if(myCheck == true){
var myLight : Light = gameObject.Find("alvo_luz_certo").GetComponent(Light);
myLight.color = trueColor;
}
else if (myCheck == false){
gameObject.Find("alvo_luz_certo").GetComponent(Light).color = falseColor;
}
if(Input.GetKeyUp(KeyCode.L) && myCheck == true){
myCheck = false;
}
else if(Input.GetKeyUp(KeyCode.L) && myCheck == false){
myCheck = true;
}
}`