Recap: Elevator
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 18 of 85.
This recap puts the chapter together: fields the Inspector sets, kept private with [SerializeField], and read every frame in Update().
Challenge
EasyWrite the elevator's script: it rises to the top and stops there.
- Add two fields,
speedandtop, as[SerializeField] private float. The Inspector sets them to 2 and 5. - In
Update(), whiletransform.position.yis belowtop, addnew Vector3(0, speed * Time.deltaTime, 0)totransform.position.
Press Play: the elevator rises to a height of 5 and stays. Then Check.
Try it yourself
using UnityEngine;
public class Lift : MonoBehaviour
{
// speed and top: private, set in the Inspector
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