Menu
CoddyTech

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 icon

Challenge

Hard

Build a staircase of 6 steps.

  1. Write float StepHeight(int number) that returns number * 0.5f + 0.25f.
  2. Write void PlaceStep(int number): a cube named Step at new Vector3(number, StepHeight(number), 0), sized new Vector3(1, 0.5f, 1).
  3. In Start(), place steps 0 to 5 with a for loop, then log Built 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.
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unity Scripting Basics

Practice on your own: Unity playground