menu_bookDocumentation
TemporaryEffect Component: Auto-Destroy, ITemporaryEffect, BecomeOrphan, and Editor Behavior
TemporaryEffect Component: Auto-Destroy with ITemporaryEffect Support
TemporaryEffect destroys a GameObject after a set time, optionally waiting for child effects (particles, sounds) to finish first.Basic Usage
CSHARP
// Destroy a GameObject after 3 seconds
var go = GameObject.Clone( explosionPrefab, WorldPosition );
var te = go.AddComponent<TemporaryEffect>();
te.DestroyAfterSeconds = 3.0f;
te.WaitForChildEffects = true; // wait for particles/sounds to finishProperties
CSHARP
te.DestroyAfterSeconds = 1.0f; // minimum lifetime
te.WaitForChildEffects = true; // wait for ITemporaryEffect components to finish
te.BecomeOrphan = false; // if true, detach from parent when parent is destroyedITemporaryEffect Interface
Components that implement ITemporaryEffect can signal when they're still active. TemporaryEffect won't destroy the GameObject until all ITemporaryEffect components in the hierarchy return IsActive = false.
CSHARP
public class MyParticleEffect : Component, ITemporaryEffect
{
public bool IsActive => _particles.IsValid() && !_particles.Finished;
}BecomeOrphan Pattern
When BecomeOrphan = true, if the parent GameObject is destroyed, the effect detaches itself (becomes a root object) and disables any looping effects. This is useful for muzzle flashes or hit effects that should finish playing even after the weapon/character is destroyed.
CSHARP
// Spawn a muzzle flash that survives weapon destruction
var flash = GameObject.Clone( muzzleFlashPrefab, muzzleTransform );
var te = flash.AddComponent<TemporaryEffect>();
te.DestroyAfterSeconds = 0.5f;
te.BecomeOrphan = true;Editor Behavior
In editor scenes, TemporaryEffect only applies to objects with GameObjectFlags.NotSaved or GameObjectFlags.Hidden. This prevents it from destroying objects you're editing.
Static Helper
CSHARP
// Manually orphan all TemporaryEffect children and disable looping
TemporaryEffect.CreateOrphans( parentGameObject, disableLooping: true );
Was this helpful?