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
MediumMake Space set off a burst of 8 sparks.
- On
Input.GetKeyDown(KeyCode.Space), run aforloop 8 times. - In it, spawn
sparkPrefabattransform.position + Random.onUnitSphere * 2f, withQuaternion.identity. - 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
}
}
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 Switch11Prefabs and Spawning
Prefabs and InstantiateSpawn on a TimerSpawn in a GridLists of ObjectsTake Back the NewestDestroyRandom SpawnsRecap: Fireworks6Transforms 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