- Home /
Help With Accessing Lights!
Hello, I've been making a game and in this game I have 3 point lights attached to a weapon. To do that, I made a container that is a child to the main camera called "Phasical", the name of the weapon. I then made the model, and the 3 lights child objects of the container. Here is an image of it if I didn't properly explain how it looked:
So, I wanted to be able to switch between the 3 lights so only one light was on at a time. Easy, I would just create a script that would change a variable, and then make a switch-case that would turn on one light and turn off the other two based on the case. This is where I ran into a some problems. I made the following script (this script is attached to the "Phasical" container that is a child to "Main Camera", the one that is highlighted.):
using UnityEngine;
using System.Collections;
public class LightControl : MonoBehaviour {
public static int LightSelected = 1; //The variable that will tell what light is active. is global for enemy AI to access
void OnGUI() {
Event e = Event.current; //create a new event
if (e.button == 1 && e.isMouse) { //If the left mouse button is down, and it is a mouse button, then...
LightSelected += 1; //We increase our selection by 1
if (LightSelected >= 3) { //We check to make sure that our variable doesn't go above 3
LightSelected = 1; //So we set it at 1 if it does
} else {
if (LightSelected < 1) { //If our variable some how goes below 1
LightSelected = 1; //We set it to 1.
}
}
}
}
void Update () {
switch(LightSelected)
{
case 1:
RedLight.enabled = RedLight.enabled;
BlueLight.enabled = !BlueLight.enabled;
YellowLight.enabled = !YellowLight.enabled;
break;
case 2:
RedLight.enabled = !RedLight.enabled;
BlueLight.enabled = BlueLight.enabled;
YellowLight.enabled = !YellowLight.enabled;
break;
case 3:
RedLight.enabled = !RedLight.enabled;
BlueLight.enabled = !BlueLight.enabled;
YellowLight.enabled = YellowLight.enabled;
break;
}
}
}
Trying this, I am presented with 18 errors all like the one below, but with different names rather than YellowLight.
Assets/LightControl.cs(28,33): error CS0103: The name `YellowLight' does not exist in the current context
I figured out I got 18 errors because of the 18 references to the light names. So, how can I enable and disable these lights without getting a "does not exist in the current context" error?
Thanks!
Brian
Answer by aldonaletto · Jun 04, 2011 at 01:56 PM
You must have 3 Transform variables to store the light instances, then fill them at the Start function using transform.Find, like this:
red = transform.Find("RedLight");
You can refer to the lights after that using these variables:
red.enabled = true;
NOTE: these variables are transforms, so you can access any property or variable of the standard Transform.
That didn't seem to solve it. I added a transform.Find for each color in the start:
void Start() { red = transform.Find("RedLight"); blue = transform.Find("BlueLight"); yellow = transform.Find("YellowLight"); }
but now im getting errors as before but ins$$anonymous$$d of "RedLight" it says "red".
It's because enabled is not a transform property or variable. What exaclty do you want this enabled thing to do: turn the light on and off? NOTE: Have you declared the new variables?
private Transform red;
private Transform blue;
private Transform yellow;
: void Start(){ : :
Yes, as I originally said, I need to turn lights on and off based on the LightSelected integer.
Thank you aldonaletto, that fixed half the problem! now I need to find a substitute for enabled seeing as it isn't a property or variable of transform.
I was able to use the property active to achieve what I wanted! Thank you aldonaletto.