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
MediumWrite the light switch: Space switches the lamp, and the switch shows its state.
- Keep the switch's own
Rendererin a field, set inStart(). - On Space, flip the Light component:
lamp.enabled = !lamp.enabled;. - Color the switch
Color.greenwhile the lamp is on andColor.redwhile 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()
{
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unity Scripting Basics
1C# Basics in Unity
Hello, UnityVariablesDecimals Need an fText in the ConsoleVariables That RememberIf and ElseCompare NumbersRecap: Coin Collector2Make It Move
Place the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube5Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot8GameObjects and Components
References in the InspectorGet a ComponentMissing ComponentsAdd a ComponentRequireComponentShow and HideRecap: Light Switch6Transforms and Hierarchy
Parents and ChildrenHoist the FlagScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon9Project: Catch the Star
The CatcherA Falling StarPractice on your own: Unity playground