Let’s create an Elevator in Unity
Part 1 — Press the button and change color
In this article we will change the color of the elevator button when the player presses it.
Split up functionality for easier and faster development
We want various functions for our elevator. We want a pressable button that changes color, a requirement of enough collectables collected and a elevator that only starts moving when called. A simple thing like an elevator in a game is made up of many smaller things. Best practice is to split up tasks like these. This way each function gets tested separately and we can focus on one thing at a time.
Elevator Panel in Editor
On our elevator panel we got a collider setup so it can detect when the player is close.
The elevator button/light is a separate game object.
We assign this elevator light/call button through the inspector.
ElevatorPanel Script
In our elevator panel script we got the following code.
[SerializeField] private Renderer _elevatorLight; private void OnTriggerStay(Collider other)
{
if (other.tag == “Player”)
{
if (Input.GetKeyDown(KeyCode.E))
{
//turn light
_elevatorLight.material.color = Color.blue;
}
}
}