FactorableObject

Abstract plain C# object base class for non-Unity instances managed by a ScriptableFactory. Implements IFactorable with a thread-safe instance ID counter and no Unity scene management. Subclass this for pooled pure-logic or data objects that do not need a GameObject or Transform.

Definition

Namespace: Paragon.Core.ScriptableFactory Assembly: Paragon.dll

[Serializable]
public abstract class FactorableObject : ParagonObject, IFactorable

Inherits: ParagonObject (implements IDisposable, IDebugObject) Implements: IFactorable

Generic Variant

[Serializable]
public abstract class FactorableObject<TData> : FactorableObject, IFactorable<TData>
    where TData : FactorableData

Implements additionally: IFactorable<TData>

Remarks

FactorableObject is the lightweight alternative to FactorableBehaviour. It extends ParagonObject (a [Serializable] C# class with IDisposable and debug support) and adds factory lifecycle management. Since there is no GameObject, spawn/despawn are purely state transitions — no activation, parenting, or scene manipulation.

Instance IDs

Unlike Unity objects, plain C# objects have no built-in GetInstanceID(). FactorableObject uses a static Interlocked.Increment counter to assign unique IDs during OnInstantiate(), ensuring thread safety.

Destruction

FactorableObject.Destroy() calls the base ParagonObject.Destroy(), which triggers OnDestroy() and IDisposable.Dispose(). The OnDestroy() override auto-despawns if still spawned and notifies the factory.

Quick Lookup

Goal
How

Create a pooled C# object

Subclass FactorableObject, override OnSpawn()

Add data initialization

Subclass FactorableObject<TData>, override InitializeData(TData)

Return to pool from self

Call Despawn()

Permanently destroy

Call Destroy()

Check if active

Read IsSpawned

Get unique ID

Call GetInstanceID()

Setup on first creation

Override OnInstantiate()

Properties

IsSpawned

Returns true while the object is spawned and active.

Methods

Despawn

Returns this object to its owning factory's pool. No-op if the object has no factory reference.

Destroy

Permanently destroys this object. Calls ParagonObject.Destroy(), which triggers OnDestroy() and disposes resources.

GetInstanceID

Returns the unique instance ID assigned during OnInstantiate(). Uses a thread-safe static counter.

Returns: Unique integer identifier for this instance.

Extension Points

Optional Overrides

Method
Called When
Purpose

OnInstantiate()

Factory creates instance (once)

One-time setup, allocate resources

OnSpawn()

Object activated from pool

Initialize per-spawn state

OnDespawn()

Object returned to pool

Reset state, release per-spawn resources

OnDestroy()

Object permanently destroyed

Final cleanup (auto-despawns if still spawned)

Required Overrides (Generic Variant)

Method
Purpose

InitializeData(TData data)

Configure object from data record (abstract in FactorableObject<TData>)

Implementation Requirements

When subclassing FactorableObject, you MUST:

  1. Mark the class with [Serializable] for factory serialization support

When subclassing FactorableObject<TData>, you MUST additionally:

  1. Override InitializeData(TData data) (abstract)

You SHOULD:

  • Perform expensive allocations in OnInstantiate() (runs once, not per spawn)

  • Reset all mutable state in OnDespawn() for clean pool re-use

  • Call base.OnDestroy() if you override OnDestroy()

You SHOULD NOT:

  • Rely on Unity lifecycle callbacks (Awake, Start, Update) — this is a plain C# object

  • Create instances with new outside the factory — the lifecycle hooks will not be called

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation
circle-exclamation

Examples

Pooled Data Object

Data-Driven Logic Object

See Also

Last updated