C# Practice: Switching Colors
Objective: Use a switch statement to change the color of the cube with specific number keys.
For this one we’ll need to create a cube object, and create a script for it. In the script we’ll need a gameobject variable that we can access in the inspector, and then drag the cube into the box so we can make changes to its material.
Now we want to make another, private variable that will represent the assigned values for our colors. We also need to make an if statement to assign the keycodes to these values.
Now with a switch statement, we can check the value of the _color variable and assign the corresponding color using GetComponent.
Now when we press a number key from 1–4, the cube will change to either blue, red, green, or black. If any other key is pressed, it’ll turn white!