◀ 2.32.42.5 ▶

2.4 — Prefabs, Spawning & Playtesting

You have a working coin, spike and potion — singular. Games need dozens of each. Prefabs are the factory: build once, stamp many, change the master and every copy updates. Then the professional ritual that runs the rest of your year: playtesting.

In this lesson you will be able to answer“how do I spawn lots of the same object?”Outline only
Break It Down~10 minOutline only

I want a hundred coins in my level, and I am not dragging each one in by hand. The game should make them while it runs — and every one of them has to obey a setup I can still change in one place.

First, before any code: cut that into parts small enough that you could look each one up on its own.

  1. Name the thing that gets copiedwhat does this part do?Read: 2.1 and 2.3 — the prefabs you built there
  2. Hold a reference to that prefabwhat does this part do?Read: the GameObject row of this lesson's reference table
  3. Give the spawner a body to live onwhat does this part do?Read: 1.3 — Your First Script, and the empty GameObject you attach the spawner to in this lesson's Build Lab
  4. Notice the key presswhat does this part do?Read: the Input.GetKeyDown(KeyCode key) row of this lesson's reference table
  5. Clone the prefab into the worldwhat does this part do?Read: the two Instantiate(...) rows of this lesson's reference table
  6. Know what a clone is notwhat does this part do?Read: the Instantiate(Object original, Vector3 position, Quaternion rotation) row of this lesson's reference table, and the press-Play-then-press-Stop test in the Build Lab

The split is the difference between a definition and an event. The prefab is the definition, the slot is your reference to it, and the clone call turns a definition into an object that exists for a few seconds. Only one part of the list is new code; the rest is file, field, and object placement you have done since 1.3.

The split is finished when every part names something you could point at in the editor: a file in Assets/Prefabs, a field in the Inspector, an object in the Hierarchy, a key, a method.

Your turn

Pick two of the parts above and say in one line what each one actually does.

This spawner runs inside Update(). Why does the key test have to be the one-frame version — and what would the Hierarchy look like at the end of a 90-minute class if it were not?

In the Editor ~20 min
  1. Hierarchy → Project window → Scene view. The Overworld ships with exactly one Coin and nothing else placed. Drag a handful of Coins, several Spikes and your Potion prefab out of Assets/Prefabs and lay out something a player could walk around.
  2. Hierarchy → select any one of them. Each instance carries a blue cube icon, which marks it as a prefab instance — a stamped copy of a master.
    Hierarchy showing OverworldHierarchyOverworldOverworldPlayerSpikesSpikesSpikes2CoinCoinPotionSpawnerMain Camera
    Your Hierarchy after step 1. Every object you dragged out of Assets/Prefabs carries the same blue cube the Coin does — and Spawner, which you make in step 5, is an ordinary object: a plain square, no cube.
  3. Project window → double-click Assets/Prefabs/Spikes to open the prefab itself. Change something functional, not cosmetic: make the collider bigger, or change the trigger setting you chose in 2.2. Save, then click the back arrow to return to the scene. Every spike in the scene updated at once — one edit, every copy.
  4. Inspector → now change one instance's position or scale in the scene. That field shows in bold: that is an override, and it is anything you changed on this copy only. Master edits still propagate everywhere you did not override.
  5. Hierarchy → right-click → Create Empty, in the Overworld. Name it something you will recognize. This is the object your spawner script will live on — in the next section.
Never delete a prefab's master file while copies live in scenes. Missing-script chaos follows, in every scene that ever used it. Treat master files like production code.

Master and Copy

  1. You changed the spike's collider on the prefab and every spike in the scene changed with it. If you had changed it on one instance instead, what would the other spikes look like — and where in the Inspector would you find the evidence of what you did?
  2. The Inspector prints an overridden field in bold. Why is an override so easy to forget you made?
  3. Deleting one prefab file breaks every copy in every scene at once, even though each copy is a separate object in the Hierarchy. What does that tell you about where a copy's definition actually lives?
In the Code ~25 min

Objects do not have to be placed by hand. A game stamps them into existence while it runs — a coin from a broken crate, a bullet from a turret. Unity does that with one method, and the table below is the whole of it. There is no code here on purpose: the API is given, and the script is yours to write.

APIWhat it isWhat you getWhat it costs
Instantiate(Object original, Vector3 position, Quaternion rotation) The full form of the clone call. Returns a reference to the clone, complete with every component, tag, collider and trigger setting the prefab carries. Three arguments in a fixed order — what to clone, where, what rotation. The clone belongs to this play session only: press Stop and it is gone, because the scene file was never written to.
Instantiate(Object original) The same call with everything left out. Clones at the prefab's own position and rotation. No say in where the clone lands, so two spawns in a row sit on top of each other and look like one object.
Quaternion.identity A value, not a method. "No rotation" — the clone aligns with the world axes. This is the constant to pass as the third argument. Only the vocabulary: it is how an API written for 3D says "no rotation".
Input.GetKeyDown(KeyCode key) True on the single frame the key goes down. One clone per press, which is what a test key wants. It is a one-frame event: the version without Down is true on every frame the key is held, and one second of holding is about sixty clones.
GameObject — as a type The type that holds a whole object, prefab asset and scene instance alike. What you declare the Inspector slot as, so a designer can drag any prefab into it. It refuses a sprite, but it cannot refuse the wrong prefab — put a Spikes prefab in the slot and it spawns spikes.
Destroy(Object obj) Removes an object at the end of the frame. The cleanup you will need the moment spawned objects are meant to disappear. Final, and no undo — this is for objects that have to go during play.
The slot matters. A slot filled by dragging an instance out of the Hierarchy holds that scene object, not the asset — so it keeps stamping the old version after you edit the prefab.
Architectural specification: the script and its slot.
  • What is the file called, and where does it live? Create it in Assets/Scripts and name the class to match the file — what does Unity tell you if the two disagree?
  • A script on no object never runs. Attach it to the empty GameObject you made in the previous section. Why is a spawner its own object rather than another method on PlayerController — which of the two knows where the spawn should happen?
  • How do you expose a slot in the Inspector? What data type holds a reference to a prefab, and where does that field have to be declared so it survives from one frame to the next?
  • The script compiles whether or not the slot is filled. What happens the moment you press the spawn key with nothing dragged in — and what would you look for in the Console?
Architectural specification: the spawn.
  • Which input function tests a single key press, and which key will you reserve for testing?
  • Which three arguments does Instantiate(Object original, Vector3 position, Quaternion rotation) take, in order? Which of them is the constant meaning "no rotation"?
  • Where does the clone appear if you pass the spawner's own transform.position? Where does it appear if you leave position out? Which do you want for a test key, and why?

Press Play and press your key. Clones should appear in the Hierarchy with (Clone) after their name, and a spawned coin should be collectable exactly like the one you placed by hand. Then press Stop and confirm they are gone.

What the Clone Inherits

  1. A clone appears already carrying the prefab's tag, collider and trigger settings. What does that prove about what the clone call copies — the file, or the values currently sitting in an Inspector?
  2. You can clone from the prefab asset in Assets/Prefabs, or from a coin instance sitting in the Hierarchy. Both produce a clone. Which one still follows your edits next week, and what is the other one a snapshot of?
  3. Press the spawn key thirty times in one place. What does the Hierarchy look like, and what does that say about why a real game reuses objects instead of building new ones forever?
Playtest ~20 min

From today until June, this is the loop that makes every unit real: swap seats, play someone else's build, write what happened, fix the top three. Print the playtest sheet from Canvas (or copy its table into your journal):

ColumnWhat you write
What happenedFacts only: "died 4 times in the spike hall"
How it feltOne adjective per row: cheap, tense, confusing, satisfying
The fix I would tryOne concrete change: "move potion before the narrow hall"

The rules that make playtests honest: the tester plays and talks out loud but does not explain yet; the designer watches silently and takes notes; no defending, no arguing. Then swap. You will run this exact sheet on real data in 9.5, where the class's sheets become a dataset — and the AP exam calls evaluating and refining (CRD-2) exactly this skill.

Checkpoint — before moving on, you must be able to:

Reference Docs

Need a deeper explanation? These pages cover the same concepts:

Next: 2.5 — Physics & Stats Studio: Catch Up and Tune, where the unit settles and the movement numbers get re-measured.

0/8 answered

Answers save on this device as you type. At the end of class, click Download and upload the file to the Canvas assignment.

Saved on this device only — the Dashboard tracks your progress.