Skip to content

Commit 9b8c58a

Browse files
committed
fatal error fixed on object pooler despawn check
1 parent 745a2df commit 9b8c58a

File tree

6 files changed

+42
-50
lines changed

6 files changed

+42
-50
lines changed

.idea/.idea.Unity-Object-Pooling/.idea/contentModel.xml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.Unity-Object-Pooling/.idea/indexLayout.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.Unity-Object-Pooling/.idea/workspace.xml

Lines changed: 19 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/ObjectPooler.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private void Start()
3636
PooledObject temp = obj.AddComponent<PooledObject>();
3737
iPool = temp;
3838
}
39+
3940
iPool.PoolType = _pool[j].Tag;
4041

4142

@@ -45,12 +46,11 @@ private void Start()
4546

4647
_poolDictionary.Add(_pool[j].Tag, objectPool);
4748
}
48-
4949
}
5050

51-
public GameObject SpawnFromPool(PooledObjectType pooledObjectType, Vector3 pos, Quaternion rot, GameObject parent = null)
51+
public GameObject SpawnFromPool(PooledObjectType pooledObjectType, Vector3 pos, Quaternion rot,
52+
GameObject parent = null)
5253
{
53-
5454
if (!_poolDictionary.ContainsKey(pooledObjectType))
5555
{
5656
Debug.LogWarning("PoolObjects with Tag " + pooledObjectType + " doesn't exist ..");
@@ -93,28 +93,28 @@ public void Despawn(GameObject obj)
9393
{
9494
PooledObjectType pooledObjectType = obj.GetComponent<IPooledObject>().PoolType;
9595

96-
if (_poolDictionary.ContainsKey(pooledObjectType) && // check if there's a queued objects by that tag.
97-
_poolDictionary[pooledObjectType].Contains(gameObject)) // check if `obj` is already despawned
98-
{
96+
bool isThereAnyQueuedObjectByTheTag = _poolDictionary.ContainsKey(pooledObjectType);
97+
98+
bool isObjectAlreadyDespawned = _poolDictionary[pooledObjectType].Contains(gameObject);
9999

100100

101+
if (isThereAnyQueuedObjectByTheTag && !isObjectAlreadyDespawned)
102+
{
101103
_poolDictionary[pooledObjectType].Enqueue(obj);
102104

103105
IPooledObject iPooledObj = obj.GetComponent<IPooledObject>();
104106
if (iPooledObj != null)
105107
{
106108
iPooledObj.OnObjectDespawn();
107109
}
110+
108111
obj.transform.SetParent(_poolMasters[pooledObjectType]);
109112
obj.SetActive(false);
110-
111-
112113
}
113114
else
114115
{
115116
Debug.LogError("Trying to despawn object which is not pooled or object is already despawned !");
116117
}
117-
118118
}
119119

120120
private GameObject ExpandPool(PooledObjectType pooledObjectType, Vector3 pos, Quaternion rot)
@@ -139,7 +139,7 @@ private GameObject ExpandPool(PooledObjectType pooledObjectType, Vector3 pos, Qu
139139
IObjectPoolInitializable iInitializable = objToAdd.GetComponent<IObjectPoolInitializable>();
140140

141141
iInitializable?.Init();
142-
142+
143143
iPooledObj.OnObjectSpawn();
144144

145145

@@ -150,6 +150,5 @@ private GameObject ExpandPool(PooledObjectType pooledObjectType, Vector3 pos, Qu
150150

151151
return objToAdd;
152152
}
153-
154153
}
155154
}

Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/PooledObject.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ public class PooledObject : MonoBehaviour, IPooledObject
1111
[Inject]
1212
public virtual void Construct(ObjectPooler pooler)
1313
{
14-
Debug.Log("construct");
15-
16-
Debug.Log(pooler);
1714
Pooler = pooler;
1815
}
1916

Assets/Resources.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)