EllanJiang/GameFramework · error · GameFrameworkException

Object is invalid.

Error message

Object is invalid.

What it means

ObjectPool.Register adds an externally created object to the pool's dictionaries keyed by object Name and Target. A null obj cannot be keyed or pooled, so Register throws 'Object is invalid.' before delegating to Object<T>.Create (which has the same guard).

Solutions

  1. Null-check the object before calling Register and handle load failure separately
  2. Make the object producer non-null-guaranteed or return a failure indicator
  3. Log which loader returned null and fix that resource path

Example fix

// before
objectPool.Register(CreateObject(), false); // CreateObject() can return null
// after
var obj = CreateObject();
if (obj == null) { Log.Error("Failed to create pooled object."); return; }
objectPool.Register(obj, false);
Defensive patterns

Strategy: validation

Validate before calling

if (obj == null) { Log.Error("Refusing to register null object"); return; }
objectPool.Register(obj, spawned);

Type guard

bool IsRegisterable<T>(T obj) where T : class => obj != null;

Try / catch

try { objectPool.Register(obj, spawned); }
catch (GameFrameworkException ex) { Log.Error("ObjectPool.Register got invalid object: {0}", ex.Message); }

Prevention

When it happens

Trigger: Calling objectPool.Register(null, spawned) directly; more often indirectly when a load/factory call returned null and its result was registered without checking.

Common situations: Asset or prefab load failures returning null; async initialization completing late so the object reference is still null at Register time; upstream service returning null on cache miss.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/7618c6bbfcecf4b8. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/ObjectPool/ObjectPoolManager.ObjectPool.cs:195

                {
                    return m_Priority;
                }
                set
                {
                    m_Priority = value;
                }
            }

            /// <summary>
            /// 创建对象。
            /// </summary>
            /// <param name="obj">对象。</param>
            /// <param name="spawned">对象是否已被获取。</param>
            public void Register(T obj, bool spawned)
            {
                if (obj == null)
                {
                    throw new GameFrameworkException("Object is invalid.");
                }

                Object<T> internalObject = Object<T>.Create(obj, spawned);
                m_Objects.Add(obj.Name, internalObject);
                m_ObjectMap.Add(obj.Target, internalObject);

                if (Count > m_Capacity)
                {
                    Release();
                }
            }

            /// <summary>
            /// 检查对象。
            /// </summary>
            /// <returns>要检查的对象是否存在。</returns>
            public bool CanSpawn()
            {

View on GitHub (pinned to d0c010b051)