Enabling and Disabling Components

Tutorial

·

Beginner

·

+10XP

·

5 mins

·

(3371)

Unity Technologies

Enabling and Disabling Components

How to enable and disable components via script during runtime.

This tutorial is included in the Beginner Scripting project.

Previous: Vector Maths

Next: Activating GameObjects

Languages available:

1. Enabling and Disabling Components

using UnityEngine;
using System.Collections;

public class EnableComponents : MonoBehaviour
{
    private Light myLight;
    
    
    void Start ()
    {
        myLight = GetComponent<Light>();
    }
    
    
    void Update ()
    {
        if(Input.GetKeyUp(KeyCode.Space))
        {
            myLight.enabled = !myLight.enabled;
        }
    }
}

Complete this tutorial