Confetti spawner
Press the cannon and every player sees a burst of confetti that disappears a few seconds later. The press goes to
everyone as a network event, and each device makes its own copies with Instantiate and removes each one with
Destroy from a delayed event: nothing about the pieces is synced. See Instantiate and Destroy.
Put the script on an object with a collider. Set Confetti to an inactive object of your world: a small quad or cube with a bright material, plus a collider and Rigidbody if the pieces should fall and bounce. Muzzle and Label (a TextMeshPro text showing how many pieces are out) are optional.
ConfettiSpawner.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;
/// <summary>
/// Confetti cannon (local Instantiate and Destroy): press it and every player sees a burst of copies of
/// <c>confetti</c>, an inactive object of the world (a renderer, and a collider and Rigidbody if the pieces should fall
/// and bounce), each destroyed <c>lifetime</c> seconds later with a delayed event. The press goes to everyone as a
/// network event and each device makes its own copies: nothing about them is synced. Instantiate returns null past the
/// caps (512 live copies per world, 256 on Quest and phones; 32 calls a frame), so a burst stops there.
/// </summary>
public class ConfettiSpawner : StageTimeBehaviour
{
public GameObject confetti;
public Transform muzzle;
public TMP_Text label;
public int perBurst = 8;
public float spread = 0.4f;
public float lifetime = 3f;
// One delayed event per live piece, well under the 64 a script may have waiting.
private const int MaxPieces = 48;
private readonly GameObject[] pieces = new GameObject[MaxPieces];
private int oldest;
private int count;
private void Start()
{
Show();
}
public override void Interact()
{
SendCustomNetworkEvent(NetworkEventTarget.All, nameof(Burst));
}
public void Burst()
{
Vector3 origin = muzzle != null ? muzzle.position : transform.position;
for (int i = 0; i < perBurst && count < MaxPieces; i++)
{
GameObject piece = Instantiate(confetti, origin + Random.insideUnitSphere * spread, Random.rotation);
if (piece == null) break; // over a cap: the log says which
pieces[(oldest + count) % MaxPieces] = piece;
count++;
SendCustomEventDelayedSeconds(nameof(RemoveOldest), lifetime);
}
Show();
}
public void RemoveOldest()
{
if (count == 0) return;
Destroy(pieces[oldest]);
pieces[oldest] = null;
oldest = (oldest + 1) % MaxPieces;
count--;
Show();
}
private void Show()
{
if (label != null) label.text = count + " confetti";
}
}