Recap: Staircase
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 42 of 85.
Put the chapter together: a method that gives back a value, a method that does a job with it, and a loop that calls it. Each step of a staircase sits half a unit higher than the one before.
Challenge
HardBuild a staircase of 6 steps.
- Write
float StepHeight(int number)that returnsnumber * 0.5f + 0.25f. - Write
void PlaceStep(int number): a cube namedStepatnew Vector3(number, StepHeight(number), 0), sizednew Vector3(1, 0.5f, 1). - In
Start(), place steps 0 to 5 with aforloop, then logBuilt 6 steps.
Press Play: six steps climb to the right. Then Check.
Try it yourself
using UnityEngine;
public class Staircase : MonoBehaviour
{
void Start()
{
// Place steps 0 to 5, then write "Built 6 steps" in the Console.
}
// Write float StepHeight(int number) here.
// Write void PlaceStep(int number) here.
}
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 Collector7Loops and Methods
Repeat with forLoops Inside LoopsWrite a MethodMethods That AnswerArraysRecap: Staircase2Make 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