How to turn the Bulb light On with Script in Unity3D

How to create Light Bulb in Unity 3D.

Here is a simple example how I created Light Bulb in Unity.

First, I created a simple sphere with material settings on the picture below. All I did was simply adding emission to the material settings.

This way I got a perfect White Sphere.

And over it I added point Light with the same XYZ coordinates and those settings:

So we got our Object ready.

Now let’s start the scripting!

If you want to turn on the light on/off you can do it with this script:

using UnityEngine;

public class LightController : MonoBehaviour {


    private Light lightBulb;
    // Use this for initialization
    void Start () {
        lightBulb = GetComponent<Light>();
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            lightBulb.enabled = !lightBulb.enabled;
        }
    }
}

After pressing the “Space” button we get our working light effect.

What we could also do is hiding the Bulb object from the scene.

using UnityEngine;

public class LightController : MonoBehaviour {

    public GameObject bulbObject;
    private Light lightBulb;
    private bool bulbObjectActive = false;
    void Start () {
        lightBulb = GetComponent<Light>();
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            lightBulb.enabled = !lightBulb.enabled;
            if (bulbObjectActive) {
                bulbObject.SetActive(false);
                bulbObjectActive = false;
            } else {
            bulbObject.SetActive(true);
                bulbObjectActive = true;
            }
        }
    }
}

Don’t forget to disable the objects before running the project!

To get a better effect, we can duplicate our Sphere object. Don’t change the position of the other object! It should be exactly the same object!

We need to create a new material without emission and add to our duplicated object.

It works. Somehow.

Now we just need to disable our “disabled Bulb” object with script while turning the Light On.

using UnityEngine;

public class LightController : MonoBehaviour {

    public GameObject bulbObject;
    public GameObject disabledBulbObject;

    private Light lightBulb;
    private bool bulbObjectActive = false;

    void Start () {
        lightBulb = GetComponent<Light>();
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            lightBulb.enabled = !lightBulb.enabled;
            if (bulbObjectActive) {
                bulbObject.SetActive(false);
                bulbObjectActive = false;
                disabledBulbObject.SetActive(true);
            } else {
                bulbObject.SetActive(true);
                bulbObjectActive = true;
                disabledBulbObject.SetActive(false);
            }
        }
    }
}

And… It works!

That’s all for today. I hope this tutorial helped you achieving your goal!

If you want to learn how to do other things in Unity, then check out other tutorials on this topic:

Basic movement in Unity3D

How to make the player Object Jump in Unity 3D.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.