Recap: Light Switch
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 35 of 67.
Challenge
MediumWrite 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.greenwhile the lamp is on andColor.redwhile 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
1Your First Script
Hello, UnityPlace the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube4Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot7Project: Catch the Star
The CatcherA Falling Star5Transforms and Hierarchy
Parents and ChildrenScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon6GameObjects and Components
References in the InspectorGet a ComponentMissing ComponentsAdd a ComponentRequireComponentShow and HideRecap: Light Switch