{"record":{"id":"2478914da6b0d34d","repo":"EllanJiang/GameFramework","slug":"can-not-find-target-in-object-pool-0-target-type-is-1-target","errorCode":null,"errorMessage":"Can not find target in object pool '{0}', target type is '{1}', target value is '{2}'.","messagePattern":"Can not find target in object pool '(.+?)', target type is '(.+?)', target value is '(.+?)'\\.","errorType":"validation","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/ObjectPool/ObjectPoolManager.ObjectPool.cs","lineNumber":316,"sourceCode":"            public void Unspawn(object target)\n            {\n                if (target == null)\n                {\n                    throw new GameFrameworkException(\"Target is invalid.\");\n                }\n\n                Object<T> internalObject = GetObject(target);\n                if (internalObject != null)\n                {\n                    internalObject.Unspawn();\n                    if (Count > m_Capacity && internalObject.SpawnCount <= 0)\n                    {\n                        Release();\n                    }\n                }\n                else\n                {\n                    throw new GameFrameworkException(Utility.Text.Format(\"Can not find target in object pool '{0}', target type is '{1}', target value is '{2}'.\", new TypeNamePair(typeof(T), Name), target.GetType().FullName, target));\n                }\n            }\n\n            /// <summary>\n            /// 设置对象是否被加锁。\n            /// </summary>\n            /// <param name=\"obj\">要设置被加锁的对象。</param>\n            /// <param name=\"locked\">是否被加锁。</param>\n            public void SetLocked(T obj, bool locked)\n            {\n                if (obj == null)\n                {\n                    throw new GameFrameworkException(\"Object is invalid.\");\n                }\n\n                SetLocked(obj.Target, locked);\n            }\n","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/ObjectPool/ObjectPoolManager.ObjectPool.cs#L298-L334","documentation":"Thrown by ObjectPool<T>.Unspawn(object target) when GetObject(target) cannot find the target in this pool. The message includes the pool name (TypeNamePair of typeof(T) and Name), the target's type, and its value to help identify the mismatch. It means you tried to recycle an object into a pool that never spawned it (or a different pool owns it).","triggerScenarios":"Recycling an object into the wrong pool instance; calling Unspawn on an object created with 'new' instead of obtained via Spawn; recycling after the pool released the object (Release/ReleaseAllUnused) so the mapping no longer exists; using the same target in multiple pools.","commonSituations":"Mistyped pool names causing lookup against pool A while the object came from pool B; object still referenced after a pool Clear; duplicate reference classes where the boxed target differs from the stored one.","solutions":["Verify the object was obtained from this exact pool via Spawn and not from another pool or direct construction.","Call Unspawn on the same IObjectPool<T> instance that spawned the object; check Name/typeof(T) in the message against your registration.","Ensure the object was not already released (double-recycle) — track in-flight state and skip the second Unspawn.","If using a base-typed target, ensure the pool stores the same runtime instance (no copies/struct boxing)."],"exampleFix":"// before\nentityPool.Unspawn(bullet); // bullet actually came from bulletPool\n// after\nbulletPool.Unspawn(bullet);","handlingStrategy":"try-catch","validationCode":"if (target != null && ownerPool != null && ReferenceEquals(ownerPool, pool)) pool.Unspawn(target);","typeGuard":"bool BelongsToPool<T>(IObjectPool<T> pool, T obj) where T : ObjectBase => obj != null && pool.CanSpawn(obj.Name); // verify ownership path in your pooling layer","tryCatchPattern":"try { pool.Unspawn(target); } catch (GameFrameworkException ex) when (ex.Message.Contains(\"Can not find target in object pool\")) { Log.Error($\"Target {target} not owned by pool {pool.Name}; check spawn origin\"); }","preventionTips":["Always unspawn into the same pool instance that spawned the object","Never Unspawn objects created with 'new' instead of Spawn","Set references to null after Unspawn to catch double-recycle bugs","Keep a spawn->pool ownership map if objects move between systems"],"tags":["object-pool","resource-not-found","target-lookup"],"backgroundTag":"resource-not-found","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}