I am a beginner at Unity and want to switch off the headlights of a car. Therefor I created two spot lights and linked them. Actually, I don’t know if this is linking or if this is childing?
In my JavaScript-File I have defined a variable, like this:
var lightToToggle : GameObject;
Clicking on a button calls the function which performs follwing:
Unfortunately only the (blue marked) headlight goes on and off. Do I have to access the children (if this is a parent-child-situation). And if so, how do I get the child?
There might be a better way but you could do this -
private var rightLight : Light;
private var leftLight : Light;
private var LightR : GameObject;
private var LightL : GameObject;
function Awake(){
LightR = GameObject.Find("Right Light"); // Find the gameObject with the Right Light
LightL = GameObject.Find("Left Light"); // Find the gameObject with the Left Light
rightLight = LightR.GetComponent(Light);
leftLight = LightL.GetComponent(Light);
}
function Update{
function OnMouseDown(){
if(rightLight.enabled){
rightLight.enabled = false;
rightLight.enabled = false;
}
else{
rightLight.enabled = true;
rightLight.enabled = true;
}
}
}