Search Unity

Problem with dictionaries [SOLVED]

Discussion in 'Scripting' started by Arpi01, Aug 5, 2019.

  1. Arpi01

    Arpi01

    Joined:
    Jul 18, 2015
    Posts:
    7
    I am making a dictionary to make a object pooling system but i keep getting the error that the tag given to the dictionary dosen't exist. The error happens when calling the SpawnFromPool method, and i don't know what the problem is
    Code (CSharp):
    1. public class ObjectPooling : MonoBehaviour
    2. {
    3.     [System.Serializable]
    4.     public class Pool
    5.     {
    6.         public string tag;
    7.         public GameObject prefab;
    8.         public int size;
    9.     }
    10.  
    11.     #region singleton
    12.     public static ObjectPooling Intance;
    13.     private void Awake()
    14.     {
    15.         Intance = this;
    16.     }
    17.     #endregion
    18.  
    19.     public List<Pool> pools;
    20.     public Dictionary<string, Queue<GameObject>> poolDictionary;
    21.     public List<string> keys;
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         poolDictionary = new Dictionary<string, Queue<GameObject>>();
    26.         foreach(Pool pool in pools)
    27.         {
    28.             Queue<GameObject> objectPool = new Queue<GameObject>();
    29.             for (int i = 0; i < pool.size; i++)
    30.             {
    31.                 GameObject obj = Instantiate(pool.prefab, transform);
    32.                 obj.SetActive(false);
    33.                 objectPool.Enqueue(obj);
    34.             }
    35.             poolDictionary.Add(pool.tag, objectPool);
    36.             Debug.Log("Created pool with tag " + pool.tag);
    37.             keys.Add(pool.tag);
    38.         }
    39.     }
    40.     private Queue<GameObject> returning = new Queue<GameObject>();
    41.     public void ReturnToPool(GameObject obj, float delay)
    42.     {
    43.         returning.Enqueue(obj);
    44.         Invoke("EndLifeTime", delay);
    45.     }
    46.     private void EndLifeTime()
    47.     {
    48.         GameObject obj = returning.Dequeue();
    49.         obj.SetActive(false);
    50.     }
    51.     public GameObject SpawnFromPool(string tag, Vector3 pos, Quaternion rot)
    52.     {
    53.         Debug.Log("Lookig for tag " + tag);
    54.         if (poolDictionary.ContainsKey(tag))
    55.         {
    56.             Debug.Log("Tag dosen't exist " + tag);
    57.             return null;
    58.         }
    59.         Debug.Log("Queue has " + poolDictionary[tag].Count + " items");
    60.         GameObject obj = poolDictionary[tag].Dequeue();
    61.         Debug.Log("Depooled" + obj.name + " with tag " + tag);
    62.         obj.SetActive(true);
    63.         obj.transform.position = pos;
    64.         obj.transform.rotation = rot;
    65.         poolDictionary[tag].Enqueue(obj);
    66.         return obj;
    67.     }
    68.  
    69.  
    70. }
    And this is how i am getting the key and calling the method SpawnFromPool where the error happens
    Code (CSharp):
    1. public GameObject InstancedGO;
    2.     public float DestroyDelay = 5;
    3.     public float volume = 1;
    4.     public AudioClip clip;
    5.     public ObjectPooling pool;
    6.     public string tagDictionary;
    7.  
    8.     private List<ParticleCollisionEvent> collisionEvents = new List<ParticleCollisionEvent>();
    9.     ParticleSystem initiatorPS;
    10.     private void Start()
    11.     {
    12.         clip = InstancedGO.GetComponent<AudioSource>().clip;
    13.         pool = ObjectPooling.Intance;
    14.         tagDictionary = pool.keys[0];
    15.     }
    16.     void OnEnable()
    17.     {
    18.         collisionEvents.Clear();
    19.         initiatorPS = GetComponent<ParticleSystem>();
    20.     }
    21.     public GameObject aux;
    22.     void OnParticleCollision(GameObject other)
    23.     {
    24.         var aliveEvents = initiatorPS.GetCollisionEvents(other, collisionEvents);
    25.         for (int i = 0; i < aliveEvents; i++)
    26.         {
    27.             aux = pool.SpawnFromPool(tagDictionary, collisionEvents[i].intersection, new Quaternion());
    28.             AudioSource audioSource = aux.GetComponent<AudioSource>();
    29.             audioSource.clip = clip;
    30.             audioSource.Play();
    31.             pool.ReturnToPool(aux, DestroyDelay);
    32.             if (aux.GetComponent<AudioSource>() != null && volume!=1)
    33.             {
    34.                 aux.GetComponent<AudioSource>().volume = volume;
    35.             }
    36.         }
    37.     }
    The result from the Debug.Log(s) is:
    Created pool with tag Audio
    Lookig for tag Audio
    Tag dosen't exist Audio
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    On line 54, you don't have a "!" before your .Contains. Your Debug.Log is actually confirming that the tag does exist.
     
  3. Arpi01

    Arpi01

    Joined:
    Jul 18, 2015
    Posts:
    7
    I litteraly just noticed that... i feel so dumb, thank you