FactorableBehaviour
Abstract MonoBehaviour base class for scene objects managed by a ScriptableFactory. Implements IFactorable with full GameObject lifecycle management — deactivation on instantiate, re-parenting on spawn/despawn, and automatic scene transfer. Subclass this for any pooled object that needs a Transform, components, and scene presence.
Definition
Namespace: Paragon.Core.ScriptableFactory Assembly: Paragon.dll
public abstract class FactorableBehaviour : ParagonBehaviour, IFactorableInherits: ParagonBehaviour → SerializedMonoBehaviour Implements: IFactorable
Generic Variant
public abstract class FactorableBehaviour<TData> : FactorableBehaviour, IFactorable<TData>
where TData : FactorableDataImplements additionally: IFactorable<TData>
Remarks
FactorableBehaviour uses explicit interface implementation for IFactorable methods, meaning the lifecycle hooks (OnInstantiate, OnSpawn, OnDespawn, OnDestroy) are only callable through the IFactorable interface (i.e., by the factory). Subclasses override the protected virtual counterparts instead.
GameObject Management
Phase
gameObject.activeSelf
transform.parent
Scene
After Instantiate
false
Factory transform
Factory's scene
After Spawn
true
null (root)
Active scene (via MoveToActiveScene())
After Despawn
false
Factory transform
Factory's scene
The onDestroy flag prevents re-parenting during Unity's OnDestroy() callback (when the factory itself may be destroyed).
Quick Lookup
Create a pooled scene object
Subclass FactorableBehaviour, override OnSpawn()
Add data initialization
Subclass FactorableBehaviour<TData>, override InitializeData(TData)
Return to pool from self
Call Despawn()
Permanently destroy
Call Destroy()
Check if active
Read IsSpawned
Setup on first creation
Override OnInstantiate()
Cleanup on despawn
Override OnDespawn()
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 the underlying GameObject.
Extension Points
Optional Overrides
OnInstantiate()
Factory creates instance (once)
One-time setup, cache component references
OnSpawn()
Object activated from pool
Initialize per-spawn state, enable systems
OnDespawn()
Object returned to pool
Reset state, disable systems, release references
OnDestroy()
Unity's OnDestroy callback
Final cleanup (auto-despawns if still spawned)
Required Overrides (Generic Variant)
InitializeData(TData data)
Configure object from data record (abstract in FactorableBehaviour<TData>)
Implementation Requirements
When subclassing FactorableBehaviour, you MUST:
Mark the class as a MonoBehaviour component (inheriting from
FactorableBehaviourhandles this)Keep the class concrete (non-abstract) if it will be used as a prefab component
When subclassing FactorableBehaviour<TData>, you MUST additionally:
Override
InitializeData(TData data)(abstract)Apply data configuration before the object is used (called by factory before spawn)
You SHOULD:
Cache component references in
OnInstantiate()(runs once per instance, not per spawn)Reset all per-spawn state in
OnDespawn()to ensure clean re-use from poolAvoid calling
base.OnDestroy()unless you understand the auto-despawn behavior
You SHOULD NOT:
Call
IFactorable.OnSpawn()orIFactorable.OnDespawn()directly — these are for the factoryAssume
OnDestroy()runs beforeOnDespawn()— the base class auto-despawns firstInstantiate
FactorableBehaviourobjects withObject.Instantiate()— always use the factory
Common Pitfalls
Double-spawn assertion OnSpawn() asserts if IsSpawned is already true. This fires if the factory tries to spawn an object that was not properly despawned. Ensure every spawn has a matching despawn.
Double-despawn assertion OnDespawn() asserts if IsSpawned is false. This fires if Despawn() is called twice or on an object that was never spawned.
Manual instantiation bypasses lifecycle Objects created via Object.Instantiate() instead of the factory will have no factory reference. Despawn() becomes a no-op, and the object will not be pooled.
OnDestroy during application quit Unity calls OnDestroy() during application quit in undefined order. The onDestroy flag prevents re-parenting to a potentially-destroyed factory, but additional cleanup in your OnDestroy() override should guard against missing references.
Examples
Basic Pooled Object
Data-Driven Object
See Also
IFactorable — interface this class implements
FactorableObject — plain C# alternative (no GameObject)
FactorableScriptableObject — ScriptableObject alternative
Factorable Object — subsystem overview
FactorableData — data records for the generic variant
Last updated