Search Unity

Need help with 2D List !

Discussion in 'Scripting' started by DeathQuake, Nov 26, 2015.

  1. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [System.Serializable]
    6. public class ObjectToPool
    7. {
    8.     public string        _ObjectName;
    9.     public GameObject     _Object;
    10.     public int             _PoolCount;
    11.     public bool         _CanSpawnAboveCount = true;
    12. }
    13.  
    14. public class ObPoolManager : MonoBehaviour
    15. {
    16.     private int mMinPoolCount = 3;
    17.     private List<List<GameObject>> mObjRefList = new List<List<GameObject>> ();
    18.  
    19.     public List<ObjectToPool> _ObjectsToPool = new List<ObjectToPool>();
    20.  
    21.     void Start()
    22.     {
    23.         for(int i = 0; i < _ObjectsToPool.Count; i++)
    24.         {
    25.             mObjRefList.Add(new List<GameObject>());
    26.             for(int j = 0; j < Mathf.Max(_ObjectsToPool[i]._PoolCount,mMinPoolCount); j++)
    27.             {
    28.                 mObjRefList[i].Add(InstantiateThis(_ObjectsToPool[i]._Object));
    29.                 mObjRefList[i][j].transform.parent = gameObject.transform;
    30.                 mObjRefList[i][j].SetActive(false);
    31.             }
    32.         }
    33.     }
    34.  
    35.     public GameObject InstantiateThis(GameObject inObj)
    36.     {
    37.         return GameObject.Instantiate(inObj, Vector3.zero, Quaternion.identity) as GameObject;
    38.     }
    39.  
    40.     public virtual void ActivateObjects(string inObjName, Vector3 inPos, Quaternion inRot)
    41.     {
    42.         int? refIndex = null;
    43.         bool canSpawn = false;
    44.         GameObject objSpawn = null;
    45.         for (int i=0; i< _ObjectsToPool.Count; i++)
    46.         {
    47.             if(string.Compare(_ObjectsToPool[i]._ObjectName, inObjName) == 0)
    48.             {
    49.                 refIndex = i;
    50.                 canSpawn = _ObjectsToPool[i]._CanSpawnAboveCount;
    51.                 objSpawn = _ObjectsToPool[i]._Object;
    52.             }
    53.         }
    54.  
    55.         if (refIndex == null)
    56.         {
    57.             Debug.LogError ("Could not find object with given name to Instantiate !");
    58.         }
    59.         else
    60.         {
    61.             for(int i = 0; i < mObjRefList[refIndex.Value].Count; i++)
    62.             {
    63.                 if(mObjRefList[refIndex.Value][i].activeInHierarchy == false)
    64.                 {
    65.                     mObjRefList[refIndex.Value][i].SetActive(true);
    66.                     mObjRefList[refIndex.Value][i].transform.position = inPos;
    67.                     mObjRefList[refIndex.Value][i].transform.rotation = inRot;
    68.                     return;
    69.                 }
    70.                 else if(mObjRefList[refIndex.Value][i].activeInHierarchy == true && mObjRefList[refIndex.Value].Count - 1 == i)
    71.                 {
    72.                     if(canSpawn)
    73.                     {
    74.                         mObjRefList[refIndex.Value].Add (new GameObject());
    75.                         mObjRefList[refIndex.Value].Add(InstantiateThis(objSpawn));
    76.                         mObjRefList[refIndex.Value][i+1].transform.position = inPos;
    77.                         mObjRefList[refIndex.Value][i+1].transform.rotation = inRot;
    78.                         mObjRefList[refIndex.Value][i+1].transform.parent = gameObject.transform;
    79.  
    80.                         //GameObject tempGo = InstantiateThis(objSpawn);
    81.                         //tempGo.transform.position = inPos;
    82.                         //tempGo.transform.rotation = inRot;
    83.                         //tempGo.transform.parent = gameObject.transform;                  
    84.                     }
    85.                     else
    86.                         Debug.Log ("Cannot activate any more instance of this Object !");
    87.                 }
    88.             }
    89.         }
    90.  
    91.     }
    92. }
    93.  
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    line 74;

    have you tried
    Code (CSharp):
    1. mObjRefList[refIndex.Value].Add (InstantiateThis(new GameObject()));
    wy you are even adding an empty gameobject?

    could you also post the hole error log ?
     
  3. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    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.
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    do you get an error log in console? what does it say?
     
  5. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    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.
     
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    don't know why it should crash but just to try it out:

    Code (CSharp):
    1.  if (canSpawn) {
    2.    try {
    3.      List<GameObject> tempList = mObjRefList[refIndex];
    4.      GameObject tempGO = InstantiateThis(objSpawn);
    5.      tempList.Add(tempGO);
    6.      tempGO.transform.position = inPos;
    7.      tempGO.transform.rotation = inRot;
    8.      tempGO.transform.SetParent(gameObject.transform);
    9.    } catch (Exception e) {
    10.      Debug.Log("Error: " + e.Message);
    11.    }
    12. }

    p.s.: you don't need to write refIndex.Value , just use refIndex
     
  7. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    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
     
  8. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    hm, tried it out in my code and i hadn'T to do this :/ was using:
    Code (CSharp):
    1. int? refIndex = null;
    2. refIndex = 1;
    3. if(refIndex == 1){Debug.Log("isOne");}
    and all was fine.

    so let's debug log a little bit.

    Code (CSharp):
    1. if (canSpawn) {
    2.    try {
    3.      List<GameObject> tempList = mObjRefList[refIndex];
    4.  
    5.      if(tempList == null) Debug.Log("templist is null");
    6.      if(tempList != null) Debug.Log("Templist ist not null Count: " +tempList.Count);
    7.  
    8.      GameObject tempGO = InstantiateThis(objSpawn);
    9.      tempList.Add(tempGO);
    10.      tempGO.transform.position = inPos;
    11.      tempGO.transform.rotation = inRot;
    12.      tempGO.transform.SetParent(gameObject.transform);
    13.    } catch (Exception e) {
    14.      Debug.Log("Error: " + e.Message);
    15.    }
    16. }
    but still unity shouldn't crash at all and you should get a error log. weird :/
     
  9. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    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.
     
  10. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    what string parameters do you give ActivateObjects() ? as name
     
  11. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    The name of the object you setup in inspector
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    DeathQuake likes this.
  13. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    Omg.. thank you so much BoredMormon !!
    Thanks for opening my eyes after 2 days.. all i needed was a
    Code (CSharp):
    1. break;
    its working fine now !!
     
    Last edited: Nov 26, 2015
    Kiwasi likes this.
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You're welcome
     
  15. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    DeathQuake likes this.
  16. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64