File tree Expand file tree Collapse file tree 1 file changed +17
-4
lines changed
Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Original file line number Diff line number Diff 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 ( ) ;
You can’t perform that action at this time.
0 commit comments