Menu
CoddyTech

Recap: Light Switch

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 35 of 67.

challenge icon

Challenge

Medium

Write the light switch. The Switch's script holds the lamp's Light in its lamp field.

  • Each press of Space switches the Light off if it is on and on if it is off. Switch the Light component, not the whole lamp.
  • The switch shows the state in its own color: Color.green while the lamp is on and Color.red while it is off, from the moment the game starts.

Press Play, press Space a few times, then Check.

Try it yourself

using UnityEngine;

public class LightSwitch : MonoBehaviour
{
    public Light lamp;

    void Start()
    {
    }

    void Update()
    {
    }
}

All lessons in Unity Scripting Basics