Skip to content

Commit 47d62cc

Browse files
committed
Properly handle destroyed game objects in spawner
1 parent f4a5e42 commit 47d62cc

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Assets/Scripts/ObjectSpawner.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,23 @@ private void SpawnObject()
102102
if (spawnedObjects.Count >= maxInstances)
103103
{
104104
var recycleGo = spawnedObjects.Dequeue();
105-
recycleGo.transform.localPosition = transform.position;
106-
recycleGo.transform.localRotation = transform.localRotation;
107-
spawnedObjects.Enqueue(recycleGo);
108-
return;
105+
if(recycleGo == null)
106+
{
107+
// If the spawned object was destroyed, we create a new one
108+
// Decrement spawn count since it will be incremented when created below
109+
totalSpawnCount--;
110+
}
111+
else
112+
{
113+
#if UNITY_2021_3_OR_NEWER
114+
recycleGo.transform.SetLocalPositionAndRotation(transform.position, transform.localRotation);
115+
#else
116+
recycleGo.transform.localPosition = transform.position;
117+
recycleGo.transform.localRotation = transform.localRotation;
118+
#endif
119+
spawnedObjects.Enqueue(recycleGo);
120+
return;
121+
}
109122
}
110123

111124
var newGo = InstantiatePrefab();

0 commit comments

Comments
 (0)