Light Toggle not functioning - Array issues
I have 2 functions for turning off fires/lights in my scene. The lights off function is working properly. But the lights on is not working at all. I watch the inspector window and it does not show that it is populating the array at all. I am using tags on the items that I want to turn on and off. I am still fairly new to Unity scripting so this may be something simple to fix. I am using GUI buttons to toggle the functions and that is working properly. Any assistance would be appreciated.
public GameObject[] lights;
public GameObject[] fires;
public void lightsOff()
{
lights = GameObject.FindGameObjectsWithTag("light");
foreach (GameObject light in lights)
{
light.SetActive(false);
}
fires = GameObject.FindGameObjectsWithTag("fire");
foreach (GameObject fire in fires)
{
fire.SetActive(false);
}
}
public void lightsOn()
{
lights = GameObject.FindGameObjectsWithTag("light");
foreach (GameObject light in lights)
{
light.SetActive(true);
}
fires = GameObject.FindGameObjectsWithTag("fire");
foreach (GameObject fire in fires)
{
fire.SetActive(true);
}
}
i don't use C# but maybe you can translate this into C#, or something close to it.
basically when you use this, it's the same as a true and false. lets say this:
function Update () { // tested
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab)){
for(var a1 in lights){
a1.active = !a1.active;
}
}
}
then again, this might not even be what you were asking.
hope this was some kind of help.
Answer by nipeerc · May 31, 2016 at 03:56 AM
I found the problem. I was not aware that that you can not find game objects by tag if the object is disabled in the scene. That was why it would not turn the lights on because I had the objects disabled. I manually added them to an array in inspector and it works fine.
Your answer
Follow this Question
Related Questions
Changing script variable from another script doesn't change it in the original script? 0 Answers
How can I find a disabled/inactive GameObject? 0 Answers
Replicate entire script series, all entailed objects and variables 0 Answers
How do make an object move in a circle around the player? 1 Answer
change a created transform in a script from another one 1 Answer