Problem with Values in a dictionary

Hi,

I am currently trying to create a dictionary which lists the closest neighbours to a waypoint on my map. The waypoint is defined by an index. Each index is a seperate ‘key’ in the dictionary. The corresponding ‘Values’ are stored as a list of Game Objects.

My code however, seems to be assigning the closest neighbours of the last ‘key’ as the ‘values’ of all keys in the dictionary. This means that each ‘key’ has the same values. Any advice would be much appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class TEST : MonoBehaviour
{
    int indexes = -1;
    Dictionary<int, List<GameObject>> attachedToWaypoint = new Dictionary<int, List<GameObject>>();
    List<GameObject> gameObjectForDic = new List<GameObject>();
    GameObject[] waypoints;



    List<GameObject> newList = new List<GameObject>(); 
  



    // Use this for initialization
    void Start()
    {

    
        waypoints = GameObject.FindGameObjectsWithTag("Waypoint");



        foreach (GameObject current in waypoints) // Loops through Game objects with tag 'Waypoints"
        {
            gameObjectForDic.Clear();
            indexes += 1;

            foreach (GameObject waypointsArroundCurrent in waypoints) // used to compare the distance of neighboring waypoints 
            {
                float distanceSqr = (current.transform.position - waypointsArroundCurrent.transform.position).sqrMagnitude; // Gets distance values
                if (distanceSqr < 60) // If waypoints are within a spcific distance
                {
                    gameObjectForDic.Add(waypointsArroundCurrent); //add gameobjects to list

                }
            }

            attachedToWaypoint.Add(indexes, gameObjectForDic); // add 'gameIbjectForDic' list to dictionary as value for specified key which is defined by the 'indexes' variable
        }



        newList = attachedToWaypoint[10]; // Test the placement of neighbours

        foreach (GameObject point in newList)
        {
            GameObject cubes = GameObject.CreatePrimitive(PrimitiveType.Cube);



            cubes.transform.position = point.transform.position;
            cubes.transform.GetComponent<Renderer>().material.color = Color.red;
        }

}



          
}

I suspect its because you keep clearing the same list every iteration, whereas you want each index to have its own list

1 Like

I have solved the issue. The problem was that the dictionary was referencing to, and doesnt explicitly store the values like a list. This is due to a bad understanding on how dictionaries work from my part. If anyone ever faces the same issue, this is my updated code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class TEST : MonoBehaviour
{
    int indexes = -1;
    Dictionary<int, List<GameObject>> attachedToWaypoint = new Dictionary<int, List<GameObject>>();
    List<GameObject> gameObjectForDic = new List<GameObject>();
    GameObject[] waypoints;
    List<List<GameObject>> nodeData = new List<List<GameObject>>();


    List<GameObject> newList = new List<GameObject>();


  
    // Use this for initialization
    void Start()
    {
      waypoints = GameObject.FindGameObjectsWithTag("Waypoint");

        storeNodesInDictionary();
        testing();
    }

    void storeNodesInDictionary()
    {
        for (int i = 0; i < waypoints.Length; i++)
        {

            nodeData.Add(new List<GameObject>());
            indexes += 1;

            for (int j = 0; j < waypoints.Length; j++)
            {
                float distanceSqr = (waypoints[i].transform.position - waypoints[j].transform.position).sqrMagnitude; // Gets distance values
                if (distanceSqr < 60 && waypoints[i] != waypoints[j]) // Is waypoints are within a spcific distance
                {
                    nodeData[i].Add(waypoints[j]);

                }
            }

            attachedToWaypoint.Add(indexes, nodeData[i]);                       
        }

    }

Thank you for the explanation, you were right!

That still looks a bit overcomplicated
instead of creating a new list and adding it to a list-of-lists, just add the new list direct to the dictionary

1 Like