Menu
CoddyTech

Recap: Light Switch

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 49 of 85.

This recap puts the chapter together: a reference filled in the Inspector, a component fetched once with GetComponent, and a component switched on and off with enabled.

challenge icon

Challenge

Medium

Write the light switch: Space switches the lamp, and the switch shows its state.

  1. Keep the switch's own Renderer in a field, set in Start().
  2. On Space, flip the Light component: lamp.enabled = !lamp.enabled;.
  3. Color the switch Color.green while the lamp is on and Color.red while it is off, from the start and after every press.

Press Play and press Space: the lamp goes off and the switch turns red. Then Check.

Try it yourself

using UnityEngine;

public class LightSwitch : MonoBehaviour
{
    public Light lamp;

    void Start()
    {
    }

    void Update()
    {
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unity Scripting Basics

Practice on your own: Unity playground