Loading a scene with an object pool gives trouble

Hi, I’m doing an asteroids game for study purposes, the asteroids, bullets and extra lives are spawned using object pooling with a singleton. Everything works fine, but when I finish a game and try replay, I get this when trying to shoot.

MissingReferenceException: The object of type 'UnityEngine.Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object+MarshalledUnityObject.TryThrowEditorNullExceptionObject (UnityEngine.Object unityObj, System.String parameterName) (at <b5bf0c891ea345fe93688f835df32fdc>:0)
UnityEngine.Bindings.ThrowHelper.ThrowNullReferenceException (System.Object obj) (at <b5bf0c891ea345fe93688f835df32fdc>:0)
UnityEngine.Transform.SetParent (UnityEngine.Transform parent, System.Boolean worldPositionStays) (at <b5bf0c891ea345fe93688f835df32fdc>:0)
UnityEngine.Transform.SetParent (UnityEngine.Transform p) (at <b5bf0c891ea345fe93688f835df32fdc>:0)
UnityEngine.Transform.set_parentInternal (UnityEngine.Transform value) (at <b5bf0c891ea345fe93688f835df32fdc>:0)
UnityEngine.Transform.set_parent (UnityEngine.Transform value) (at <b5bf0c891ea345fe93688f835df32fdc>:0)
SpawnPool+PrefabPool.SpawnInstance (UnityEngine.Vector3 position, UnityEngine.Quaternion rotation, UnityEngine.Vector3 scale, UnityEngine.Transform parent) (at Assets/_Data/Scripts/Logic/SpawnPool.cs:33)
SpawnPool.Spawn (UnityEngine.Transform prefab, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation, UnityEngine.Vector3 scale, UnityEngine.Transform parent) (at Assets/_Data/Scripts/Logic/SpawnPool.cs:133)
SpawnPool.Spawn (UnityEngine.Transform prefab, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at Assets/_Data/Scripts/Logic/SpawnPool.cs:127)
PlayerController.OnFire (UnityEngine.InputSystem.InputValue inputValue) (at Assets/_Data/Scripts/Player/PlayerController.cs:92)
UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)

Additionally, asteroids and lives do not appear either.

This is how I manage Scenes:



I would appreciate a lot some help on this topic :slight_smile:

The error stack trace is showing you the scripts where the error is coming from, and it doesn’t have anything to do with your scene loading code.

The issue is likely that your pooling singleton is holding onto a reference onto something from the scene that has been unloaded, thus destroyed. Bottomline is persistent singletons should not hold onto references to transient objects.

If your singleton is DontDestroyOnLoad for example, the pooled objects should also live under its DontDestroyOnLoad hierarchy. Or, your object pooler should not be singleton, and should only live with the scene.

One this study project is hopefully teaching is that using things like object pooling come with technical complexity and debt, and you should consider whether your project actually needs them before implementing them. Namely there’s no point in implementing object pooling if you don’t have a performance concern in the first place.

I just tried it and works like a charm, thanks for the hint and the explanation.
I wanted to use object pooling not for resources, but to lear how it works, It seemed odd to me that the pool itself was in the DontDestroyOnLoad but the objects not, now this makes a lot more sense.

Again, thanks for the contribution :smiley:

1 Like