Unity 3d Lights on and off with UI buttons

Hey guys welcome back to unity tips pro.In this post we discus about lights on with ui buttons.Last post we discus about unity terrain tips and tricks for level design please check this post.Lights are important element for games.some time we need light are open with switch.For example i add a light in room so we need a switch that is use for light on or off.

unity lights on and off

Light on/off with UI buttons

First of all we need some setup to perform this task.You add some objects
1.one Plane

2.one Cube

3.Directional light color is dark like night scene.

4.Spot light on cube.Check setup on image

unity 3d lights 2019.3

5.add this script to main camera

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

public class Light : MonoBehaviour {

    public Light myLight;
// Use this for initialization
void Start () {


}
// Update is called once per frame


        public void Light () {
      
        {
            myLight.enabled = !myLight.enabled;
        }
}
}
...................................................................................................................................................................

6.We need one button that use as a switch.Go to UI and add button and button name is light on/off.

7.Click on button  go to click section and add main camera object.

8.Select public function light.

9.Light is working properly.

0 Comments