SingletonNetworkBehaviour

Abstract generic singleton base class for network behaviours that need exactly one authoritative instance in the networked scene. Combines the SingletonBehaviour pattern (find → load → create) with automatic NetworkObject.Spawn() on Start().

Definition

Namespace: Paragon.Core.Network Assembly: Paragon.dll

[SingletonSettings]
public abstract class SingletonNetworkBehaviour<T> : ParagonNetworkBehaviour
    where T : SingletonNetworkBehaviour<T>

Inheritance: NetworkBehaviourParagonNetworkBehaviourSingletonNetworkBehaviour<T>

Remarks

This class mirrors the SingletonBehaviour<T> pattern used by non-networked singletons but adds network-specific behaviour:

  1. Instance resolution follows a chain: cached instance → FindFirstObjectByType<T>()Resources.Load<T>(assetPath) → create new GameObject with component.

  2. Auto-spawn: On Start(), if the NetworkObject is not already spawned, it calls NetworkObject.Spawn() automatically. This ensures the singleton is network-visible as soon as it enters the scene.

  3. DontDestroyOnLoad support via [SingletonSettings(DontDestroyOnLoad = true)].

  4. Editor asset creation: In the editor, if an AssetPath is configured and no prefab exists, one is automatically created in Assets/Resources/.

The class uses the curiously recurring template pattern (CRTP) — T must be the concrete subclass itself.

circle-exclamation

Quick Lookup

Goal
How

Access the singleton

MySingleton.Instance

Check debug state

MySingleton.DebugEnabled

Create a network singleton

Subclass with [SingletonSettings] (see example)

Properties

Instance (static)

Returns the singleton instance, creating or finding it if necessary.

Resolution order:

  1. Cached instance field

  2. FindFirstObjectByType<T>() in the scene

  3. Resources.Load<T>(assetPath) if AssetPath is configured

  4. Create new GameObject with AddComponent<T>()

DebugEnabled (static, new)

Whether debug output is enabled for the singleton instance. Shadows the instance-level property for convenient static access.

Extension Points

Optional Overrides

Method
Purpose

Awake()

Initializes the singleton. Sets instance, applies DontDestroyOnLoad and hide flags. Call base.Awake() if overriding.

Start()

Auto-spawns the NetworkObject if not already spawned. Call base.Start() if overriding.

OnDestroy()

Clears the static instance reference. Call base.OnDestroy() if overriding.

Implementation Requirements

When subclassing SingletonNetworkBehaviour<T>, you MUST:

  1. Pass the concrete class as T (CRTP): class MyManager : SingletonNetworkBehaviour<MyManager>

  2. Apply [SingletonSettings] attribute (even with defaults)

  3. Ensure the prefab has a NetworkObject component (required for Spawn() in Start())

You SHOULD:

  • Set AssetPath in [SingletonSettings] if the singleton should be loadable from Resources

  • Set DontDestroyOnLoad = true for managers that persist across scenes

  • Call base.Awake() and base.Start() if overriding lifecycle methods

You SHOULD NOT:

  • Access Instance outside of Play Mode (assertion will fire)

  • Create multiple instances of the same singleton type (assertion will fire in Awake())

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

Examples

Creating a Network Singleton Manager

Accessing the Singleton

See Also

Last updated