Enabling and Disabling Components

Evidence

Script

using System.Collections;
using UnityEngine;

public class EnableComponents : MonoBehaviour 
{
    private Light myLight;

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

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

Screencast

What I Learned

In doing this tutorial I learned how to enable and disable components through a script which includes a toggle function through the space bar. Specifically, this tutorial showed me how to turn a light on and off through the script. One problem I had to solve was making sure my code was accurate and without errors. Otherwise this tutorial was pretty problem free.

Leave a Reply

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