Menu
CoddyTech

Recap: Fireworks

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 69 of 85.

This recap puts the chapter together: a prefab copied with Instantiate in a loop, a random place for each copy, and Destroy with a delay.

challenge icon

Challenge

Medium

Make Space set off a burst of 8 sparks.

  1. On Input.GetKeyDown(KeyCode.Space), run a for loop 8 times.
  2. In it, spawn sparkPrefab at transform.position + Random.onUnitSphere * 2f, with Quaternion.identity.
  3. Pass the new spark to Destroy(spark, 1f);.

Press Play and press Space: sparks burst all around and vanish after a second. Then Check.

Try it yourself

using UnityEngine;

public class Fireworks : MonoBehaviour
{
    public GameObject sparkPrefab;

    void Update()
    {
        // Space: 8 sparks, each 2 units away in a random direction, each gone after 1 second
    }
}
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