Unity Learn home
View Tutorial Content
Steps

Enabling and Disabling Components

Tutorial
Beginner
+10 XP
5 Mins
(3337)
Summary
How to enable and disable components via script during runtime.
This tutorial is included in the Beginner Scripting project.
Previous: Vector Maths
Select your Unity version
Last updated: November 17, 2023
2019.3
2019.2
2019.1
2018.4
2018.3
2018.2
2018.1
2017.4
2017.3
2017.2
2017.1
5.x
4.x
Language
English
Also included in

1.Enabling and Disabling Components

Video Player is loading.
Current Time 0:00
Duration 0:00
Loaded: 0%
Stream Type LIVE
Remaining Time 0:00
 
1x

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; } } }

Enabling and Disabling Components
Enabling and Disabling Components
General Tutorial Discussion
7
5
1. Enabling and Disabling Components
3
10