Prefabs and spawning
Prefabs let you save a configured object and reuse it. They are how you avoid rebuilding the same enemy, pickup, projectile, door, or UI widget over and over.
In Opal, prefab-like assets are stored in the library as blueprints.
What a prefab keeps
A prefab can include:
| Data | Examples |
|---|---|
| Object hierarchy | Parent object, child sprites, markers, hitboxes |
| Inspector fields | Name, visibility, interaction, transform defaults |
| Components | Health, Collider 2D, Inventory, Spawner, custom components |
| Object variables | Per-object state declarations |
| Object Flow | On Tap, collision, custom event rules |
| Animation | Motion clips and skeleton references |
Use prefabs whenever the same object appears more than once or must be spawned during play.
Save a prefab
- Select an object in Arrange.
- Configure its children, components, variables, motion, and Object Flow.
- Use the prefab action in the inspector to save it.
- Find the result in the library, usually under Blueprints.
Name the prefab by role:
| Weak | Better |
|---|---|
thing | CoinPickup |
enemy | SlimeEnemy |
door | DoorToLevel2 |
button | RetryButton |
Place prefab instances
Place a prefab from the asset library like any other asset. The new scene object starts with the prefab's saved configuration.
Good uses:
- Coins and pickups
- Enemy variants
- Projectiles
- Reusable UI controls
- Door/portal objects
- Spawn markers
- Hazards and traps
Instance overrides
After placing an instance, you can change it for that scene.
Common overrides:
| Override | Example |
|---|---|
| Transform | Put this coin at a different position |
| Component field | Give one slime more HP |
| Variable default | Set this door target to Level 3 |
| Visibility | Hide a bonus item until unlocked |
Use manual sync when an instance should stop following prefab updates. Use revert controls when an instance should return to the prefab defaults.
Update a prefab
When you improve the source object, update the prefab so future instances and auto-synced existing instances share the change.
Safe update workflow:
- Duplicate a scene or export a backup first.
- Change one source object.
- Update the prefab.
- Check a few existing instances.
- Play-test.
Large prefab updates can affect many scenes, so treat them like a real content change.
Spawn from Object Flow
Use the Spawn Object action to create a prefab during play.
Common graph:
On Timer
-> Spawn Object (SlimeEnemy)or:
On Tap
-> Spawn Object (Projectile)
-> Play AudioPick the prefab from the node field. Set position or offset fields where available so the spawned object appears at the source, target, or marker you expect.
Spawn with the Spawner component
The Spawner component periodically creates a prefab at the object's position.
Use it for:
- Enemy waves
- Falling obstacles
- Repeating pickups
- Particle-like object bursts
- Test scenes that need automatic content
Typical fields include the prefab to spawn, interval, spawn-on-start behavior, and maximum spawn count. Attach the component to an empty marker object when you want a clean spawn point.
Prefab design patterns
| Pattern | Structure |
|---|---|
| Pickup | Sprite + Pickup (or On Tap Flow) + GameState / inventory write + VFX + Destroy |
| Enemy | Parent group + sprite + Health + Collider 2D + Motion + Object Flow |
| Door | Sprite + Collider sensor + target scene variable + Go To Scene |
| Projectile | Sprite + Projectile + dynamic Rigid Body 2D (CCD) + Collider 2D |
| Spawner | Empty object + Spawner component + optional visual marker hidden in play |
Debugging prefab issues
If a prefab instance behaves strangely:
- Select the instance and check whether it is auto-synced or manual.
- Compare instance component fields against the prefab.
- Check child objects, not just the root.
- Verify the prefab asset still exists in the library.
- Use Graph Explorer's prefab anatomy view for complex templates.