Switch multiple lights On/Off ?
Hi, I'm a beginner using Unity and C# so please bare with me if I don't have the tech vocabulary.
So, I have a room with lights on walls and sealing.
The Character (first-person) must interact/hit a cube (switch) on the wall to turn lights on/off.
Because I have multiple lights in this scene, the lights from the room are child to an empty game object with "roomLights" tag.
The character must hit the switch to turn off/on only the room lights (event/trigger to target the roomLights tag).
I've watched some tutorials but some of them are old and using javascript which is a no-no for me.
Any directions or maybe the C# sample would be helpful.
Thank you.
Answer by UrielKane · Feb 25, 2018 at 07:14 PM
There are many diferent ways of doing that. The code line you are interested on is "gameObject.SetActive(true); and gameObject.SetActive(false);" you can also "lightcomponent.enable = true; or = false;" I usually use setactive becouse i become a little paranoid sometimes having a component still working even if i disable it.
And this being said, you have to figure out how to reach that code, you can do it from a simple trigger to a complex Raycast system. If i'm not wrong you can do it with OnMouseDawn () function to. It was a long since the last time i use OnMouseDawn so i cant recomend it. But if you want the character not only to be close to the switch but also looking at it, you should use either OnMouseDawn or Raycast.
Answer by imagoculturae · Nov 12, 2019 at 11:43 AM
So I have this script applied to an object switch that can switch on/off only one light, how can I modify it so that I can insert multiple lights in the editor? Do I need to use an array? How is it done in the editor? Many thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
public class LightSwitchObj : MonoBehaviour
{
public GameObject linkedLight;
private bool lightStatus = false;
void OnMouseDown()
{
if (lightStatus == false)
{
linkedLight.SetActive(true);
lightStatus = true;
}
else if(lightStatus == true)
{
linkedLight.SetActive(false);
lightStatus = false;
}
}
}
If you want the lights to be toggled on and off according to their previous state
using UnityEngine;
public class LightSwitchObj : $$anonymous$$onoBehaviour
{
public GameObject[] lights;
void On$$anonymous$$ouseDown()
{
foreach(var light in lights)
{
light.SetActive(!light.activeSelf);
}
}
}
If you want the lights to "share the same state" (all enabled / disabled at the same time)
using UnityEngine;
public class LightSwitchObj : $$anonymous$$onoBehaviour
{
public GameObject[] lights;
private bool lightEnabled;
void On$$anonymous$$ouseDown()
{
lightEnabled = !lightEnabled;
foreach(var light in lights)
{
light.SetActive(lightEnabled);
}
}
}
If you want only one light to be enabled at a time
using UnityEngine;
public class LightSwitchObj : $$anonymous$$onoBehaviour
{
public GameObject[] lights;
private int enabledLightIndex;
void On$$anonymous$$ouseDown()
{
enabledLightIndex = (enabledLightIndex + 1) % lights.Length;
for(int lightIndex = 0 ; lightIndex < lights.Length ; ++lightIndex)
{
lights[lightIndex].SetActive(lightIndex == enabledLightIndex);
}
}
}
$$anonymous$$any thanks! The second case will do for me. However I need also to rotate the switch a bit, where do I insert this code into your sintax? Is there also a way to modify the intesity of the lights all at the same time? I am planning to use a fader switch. Sorry for asking so many quaestions, but this will complete my prefab, I can share it after this...
rotswitch.transform.Rotate(0, 0, 10);
...
rotswitch.transform.Rotate(0, 0, -10);
you can call light.transform.Rotate(0, 0, 10)
inside the foreach
, but this will rotate all the lights each time the lights are enabled / disabled.
To change the intensity, call light.GetComponent<Light>().intensity = /* your value*/;
inside the foreach. But again, it will change the intensity for all the lights each time the lights are enabled / disabled.