menu_bookDocumentation
IsValid Pattern: Safe Null and Destroyed Object Checks
Destroying a GameObject or Component doesn't erase the C# reference — the variable still holds a non-null but dead object. A plain != null check will pass on destroyed objects, leading to exceptions or silent failures.
The Problem
CSHARP
// ❌ Wrong — reference is still non-null after Destroy()
if ( myObject != null )
{
myObject.DoSomething(); // might throw or do nothing
}
// ✅ Correct — safe even if myObject is null
if ( myObject.IsValid() )
{
myObject.DoSomething();
}Common Patterns
Guard before use:
CSHARP
protected override void OnUpdate()
{
if ( !_target.IsValid() ) return;
var dist = WorldPosition.Distance( _target.WorldPosition );
}Filter destroyed objects from lists:
CSHARP
_targets.RemoveAll( t => !t.IsValid() );Guard async callbacks (objects may be destroyed during await):
CSHARP
async Task ShootAfterDelay()
{
await Task.DelaySeconds( 1.0f );
if ( !this.IsValid() ) return; // component might be destroyed
FireProjectile();
}
Was this helpful?