Instantiate and Destroy
Scripts can copy an object of your world while it runs, for confetti, sparks, debris, footprints and other things that come and go:
| API | Does |
|---|---|
Instantiate(template, position, rotation) | Makes a copy of template at position and rotation (world space), active, under your world's Local Copies object, and returns it. Returns null when a rule or limit stops it |
Instantiate(template, position, rotation, parent) | The same, with the copy under parent (position and rotation still in world space). parent must be part of your world |
Destroy(copy) | Removes a copy made by Instantiate. Anything else is left alone, with a warning |
They are static methods of StageTimeBehaviour, so call them as in Unity: Instantiate(...), Destroy(...).
public GameObject spark; // an inactive object of your world
private GameObject[] pieces = new GameObject[16];
private int count;
public void Pop()
{
if (count == pieces.Length) return;
GameObject copy = Instantiate(spark, transform.position, Quaternion.identity);
if (copy == null) return; // a rule or a limit stopped it; the log says which
pieces[count++] = copy;
SendCustomEventDelayedSeconds(nameof(Clear), 2f);
}
public void Clear()
{
for (int i = 0; i < count; i++) Destroy(pieces[i]);
count = 0;
}
The copy is active even when the template isn't, so keep the template inactive if it shouldn't show on its own. Its particles and sounds with Play On Awake start when it's made. Copies go when the world unloads.
Local, not synced
Copies are local: each player's device makes its own, and nothing about them is synced (Instantiate in VRChat
is local too). To have everyone see the same thing, make the copies from an event that runs on every device, such as
a network event or
OnProjectileHit, with the same position and rotation. Work
out anything random from the event's values rather than Random if it has to match everywhere. A player who joins
later doesn't see copies made before they arrived.
For things you show many of again and again (projectiles, impacts, splats), an ST Object Pool is cheaper: its copies are made once when the world loads.
What a template can hold
The same as an ST Object Pool template: it must be part of your world, and hold only plain Unity components: renderers, colliders, rigidbodies, particles, trails, lights, audio and animators. Not allowed:
- scripts, StageTime components (pickups, seats, video players, hitboxes and so on) and anything networked: copies are made after StageTime has set up the world, so they couldn't be synced;
- Visual Effects (VFX Graph): copies would go over the world's VFX budget; use a Particle System;
- cameras, audio listeners, video players and reflection probes: each copy would cost a whole extra render or listener;
- components from outside Unity and TextMeshPro (other packages' scripts);
- more than 32 objects (the template and everything under it).
A template that breaks a rule makes Instantiate return null, with one warning in the log naming the problem. The
template is checked on every call, so changing it at runtime is fine.
Copies are sealed
A script can move a whole copy anywhere in the world with transform.SetParent, but can't move objects into or out
of a copy: that call stops the script with an error. This keeps Destroy(copy) from ever removing anything but
copied content, and keeps every copy counted towards the limits.
Copies with colliders are solid for players, and STPhysics queries and projectiles hit them.
Copies have no scripts, so they can't be clicked (Interact).
Limits
| Limit | Value |
|---|---|
| Live copies | 512 per world; 256 on Quest, Pico and phones |
| Objects in a template | 32 (the template and everything under it) |
Instantiate calls | 32 a frame (8 on Quest, Pico and phones), all scripts together |
Past a limit Instantiate returns null, with one warning. Destroy copies you no longer need, and keep your own count
so you don't rely on the limits.
Example
The Confetti spawner example makes a burst of copies on every device when someone presses it, and destroys each piece a few seconds later with a delayed event.