Do I have to have a UI gameobject and script for each Light in my house?

Hi guys,

I have a light and a switch on a wall that when you stand close enough brings up a UI light switch slider that you can control the brightness of the lights. Do i have to create a set like this for each light in my house? doesnt seem very efficient. How can I share the one element on my UI for all lights? Is this possible?
here is my light switch code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PassageLightController : MonoBehaviour
{
    public Light passageLight1;
    public Light passageLight2;
    public Slider lightValue;
    public GameObject uiLightControl;

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            uiLightControl.SetActive(true);
        }
    }

    private void Update()
    {
        passageLight1.intensity = lightValue.value;
        passageLight2.intensity = lightValue.value;
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            uiLightControl.SetActive(false);
        }
    }
  




}

You could create a static variable that all your lights can access, then have that be your UI switch.

1 Like

You’ll definitely need a way to link up a particular switch to a particular light or set of lights. But you can pretty easily create one script for this and reuse it many times.

The UI slider bit could be part of a separate script that you have attached to the player.

1 Like