Hi Guys, I recently started working on a little class for object pooling. I have got to a point where I can setup any number of objects to pool with a initial count. I also wanted extra objects to be pooled on demand. The below code works all good except it crashes at a point where i try to add an extra element to the 2D List (On Line 74). Can someone please explain where i am going wrong. Iv been trying to figure this out for 2 days now. Thanks in advance Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; [System.Serializable] public class ObjectToPool { public string _ObjectName; public GameObject _Object; public int _PoolCount; public bool _CanSpawnAboveCount = true; } public class ObPoolManager : MonoBehaviour { private int mMinPoolCount = 3; private List<List<GameObject>> mObjRefList = new List<List<GameObject>> (); public List<ObjectToPool> _ObjectsToPool = new List<ObjectToPool>(); void Start() { for(int i = 0; i < _ObjectsToPool.Count; i++) { mObjRefList.Add(new List<GameObject>()); for(int j = 0; j < Mathf.Max(_ObjectsToPool[i]._PoolCount,mMinPoolCount); j++) { mObjRefList[i].Add(InstantiateThis(_ObjectsToPool[i]._Object)); mObjRefList[i][j].transform.parent = gameObject.transform; mObjRefList[i][j].SetActive(false); } } } public GameObject InstantiateThis(GameObject inObj) { return GameObject.Instantiate(inObj, Vector3.zero, Quaternion.identity) as GameObject; } public virtual void ActivateObjects(string inObjName, Vector3 inPos, Quaternion inRot) { int? refIndex = null; bool canSpawn = false; GameObject objSpawn = null; for (int i=0; i< _ObjectsToPool.Count; i++) { if(string.Compare(_ObjectsToPool[i]._ObjectName, inObjName) == 0) { refIndex = i; canSpawn = _ObjectsToPool[i]._CanSpawnAboveCount; objSpawn = _ObjectsToPool[i]._Object; } } if (refIndex == null) { Debug.LogError ("Could not find object with given name to Instantiate !"); } else { for(int i = 0; i < mObjRefList[refIndex.Value].Count; i++) { if(mObjRefList[refIndex.Value][i].activeInHierarchy == false) { mObjRefList[refIndex.Value][i].SetActive(true); mObjRefList[refIndex.Value][i].transform.position = inPos; mObjRefList[refIndex.Value][i].transform.rotation = inRot; return; } else if(mObjRefList[refIndex.Value][i].activeInHierarchy == true && mObjRefList[refIndex.Value].Count - 1 == i) { if(canSpawn) { mObjRefList[refIndex.Value].Add (new GameObject()); mObjRefList[refIndex.Value].Add(InstantiateThis(objSpawn)); mObjRefList[refIndex.Value][i+1].transform.position = inPos; mObjRefList[refIndex.Value][i+1].transform.rotation = inRot; mObjRefList[refIndex.Value][i+1].transform.parent = gameObject.transform; //GameObject tempGo = InstantiateThis(objSpawn); //tempGo.transform.position = inPos; //tempGo.transform.rotation = inRot; //tempGo.transform.parent = gameObject.transform; } else Debug.Log ("Cannot activate any more instance of this Object !"); } } } } }
line 74; have you tried Code (CSharp): mObjRefList[refIndex.Value].Add (InstantiateThis(new GameObject())); wy you are even adding an empty gameobject? could you also post the hole error log ?
Hey Martinmr, Unity crashes when i try to add a extra element into an already existing 2D list. even if i remove line 74.. it will still crash on 75. I just wanted to know how to add an element to the 2D list because the way i try it. It is not working Adding a empty GameObject in line 74 is just a try to see if it works.
The unity editor freezes and i had to force quit. I am guessing it happens because i'm doing something wrong in accessing the List. There are no error logs in Editor.txt from "%localappdata%\Unity\Editor\ " either.
don't know why it should crash but just to try it out: Code (CSharp): if (canSpawn) { try { List<GameObject> tempList = mObjRefList[refIndex]; GameObject tempGO = InstantiateThis(objSpawn); tempList.Add(tempGO); tempGO.transform.position = inPos; tempGO.transform.rotation = inRot; tempGO.transform.SetParent(gameObject.transform); } catch (Exception e) { Debug.Log("Error: " + e.Message); } } p.s.: you don't need to write refIndex.Value , just use refIndex
Thanks for the help, but its still crashing whenever we try to add a element into the list. Your code works if i just comment out the tempList.Add(tempGo) part, but then i do not have reference to the newly created object And i get error CS1503: Argument `#1' cannot convert `int?' expression to type `int' if i don't use refIndex.Value
hm, tried it out in my code and i hadn'T to do this :/ was using: Code (CSharp): int? refIndex = null; refIndex = 1; if(refIndex == 1){Debug.Log("isOne");} and all was fine. so let's debug log a little bit. Code (CSharp): if (canSpawn) { try { List<GameObject> tempList = mObjRefList[refIndex]; if(tempList == null) Debug.Log("templist is null"); if(tempList != null) Debug.Log("Templist ist not null Count: " +tempList.Count); GameObject tempGO = InstantiateThis(objSpawn); tempList.Add(tempGO); tempGO.transform.position = inPos; tempGO.transform.rotation = inRot; tempGO.transform.SetParent(gameObject.transform); } catch (Exception e) { Debug.Log("Error: " + e.Message); } } but still unity shouldn't crash at all and you should get a error log. weird :/
Can you do me a favor and try using my code to see if your Unity crashes. I am sorry if im asking too much. Just add this ObPoolManager.cs to a gameobject, setup the inspector for ObjectsToPool with a cube as prefab. Call ActivateObjects() from other script on Input.GetMouseButtonDown(0) Thanks.
This signifys an infinite loop. That's the only reason I've ever seen Unity freeze up requiring a force quit. So start looking for the loop. If I had to guess I would say you are adding items to the list while you are iterating through it with a for loop. If you add an item every iteration you will never reach the end of the loop. For reference you are using a nested list, not a 2D list. It's late and it threw me off for a second.
Omg.. thank you so much BoredMormon !! Thanks for opening my eyes after 2 days.. all i needed was a Code (CSharp): break; its working fine now !!