{"record":{"id":"364d7604bd83b9da","repo":"clockworklabs/SpacetimeDB","slug":"spacetimedbnetworkmanager-is-a-singleton-and-shoul","errorCode":null,"errorMessage":"SpacetimeDBNetworkManager is a singleton and should only be attached once.","messagePattern":"SpacetimeDBNetworkManager is a singleton and should only be attached once\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sdks/csharp/src/SpacetimeDBNetworkManager.cs","lineNumber":36,"sourceCode":"        /// AutoStaticsCleanup and NoAutoStaticsCleanup is only supported in Unity 6+\n        /// </summary>\n        /// <remarks>\n        /// See the <see href=\"https://docs.unity3d.com/6000.5/Documentation/Manual/domain-reloading.html\">Unity Domain Reloading Manual</see> \n        /// and the <see href=\"https://docs.unity3d.com/6000.5/Documentation/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html\">RuntimeInitializeOnLoadMethodAttribute API Docs</see> for details.\n        /// </remarks>\n        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]\n        private static void ResetStaticFields()\n        {\n            _instance = null;\n        }\n\n        public void Awake()\n        {\n            // Ensure that users don't create several SpacetimeDBNetworkManager instances.\n            // We're using a global (static) list of active connections and we don't want several instances to walk over it several times.\n            if (_instance != null)\n            {\n                throw new InvalidOperationException(\"SpacetimeDBNetworkManager is a singleton and should only be attached once.\");\n            }\n            else\n            {\n                _instance = this;\n            }\n        }\n\n        private readonly List<IDbConnection> activeConnections = new();\n\n        public bool AddConnection(IDbConnection conn)\n        {\n            if (activeConnections.Contains(conn))\n            {\n                return false;\n            }\n            activeConnections.Add(conn);\n            return true;\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/sdks/csharp/src/SpacetimeDBNetworkManager.cs#L18-L54","documentation":"Unity-only MonoBehaviour that ticks all active SpacetimeDB connections. Awake guards a static _instance so exactly one manager exists (its Update/OnDestroy iterate a global connection list). A second instance throws InvalidOperationException from Awake, which in Unity also disables that component. ResetStaticFields clears the static on play-mode start to survive disabled domain reloading.","triggerScenarios":"The manager GameObject exists in two simultaneously loaded scenes (additive scene loading), a prefab with the component instantiated alongside a scene copy, or a DontDestroyOnLoad instance plus a duplicate in a reloaded scene.","commonSituations":"Adding SpacetimeDBNetworkManager to both the bootstrap scene and gameplay scenes 'to be safe'; duplicating a prefab that carries it; multi-scene Unity setups.","solutions":["Search all loaded scenes for SpacetimeDBNetworkManager and keep exactly one","Bootstrap the manager once in an init scene before additive scenes load","When creating it programmatically, guard with FindObjectOfType<SpacetimeDBNetworkManager>() before AddComponent"],"exampleFix":"// before\ngameObject.AddComponent<SpacetimeDBNetworkManager>(); // throws if one already exists\n\n// after\nif (FindObjectOfType<SpacetimeDBNetworkManager>() == null)\n    gameObject.AddComponent<SpacetimeDBNetworkManager>();","handlingStrategy":"validation","validationCode":"if (FindObjectOfType<SpacetimeDBNetworkManager>() != null)\n    return; // singleton already present in a loaded scene\ngameObject.AddComponent<SpacetimeDBNetworkManager>();","typeGuard":"static bool ManagerExists() => FindObjectOfType<SpacetimeDBNetworkManager>() != null;","tryCatchPattern":"try { gameObject.AddComponent<SpacetimeDBNetworkManager>(); }\ncatch (InvalidOperationException) { /* duplicate instance: destroy this GameObject and reuse the existing manager */ }","preventionTips":["Put the manager in exactly one bootstrap scene","Before additive scene loads, assert only one instance exists","When instantiating prefabs that carry it, strip or check the component first"],"tags":["unity","singleton","monobehaviour"],"backgroundTag":"duplicate-singleton-instance","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}