#Updating dictionary values not working

1 messages · Page 1 of 1 (latest)

tulip idol
#

Hello everyong,
I'm having an issue with updating dictionary values or maybe not understanding scope properly. Brief overview, this a simple little puzzle game where you have to click and swap around cubes.

As a property of a class I have :

        private struct CubeData { public string color; public float size; public int position; };
        // Game state
        private GameObject cubeSelected;
        private Dictionary<string, CubeData> cubeState = new Dictionary<string, CubeData>
        {
            { "Cube_1_1", new CubeData { color = "orange", size = 0.8f, position = 1 } },
            { "Cube_1_2", new CubeData { color = "opaque", size = 0.5f, position = 2 } },
            { "Cube_1_3", new CubeData { color = "opaque", size = 0.3f, position = 3 } },
            { "Cube_1_4", new CubeData { color = "opaque", size = 0.4f, position = 4 } },
            { "Cube_1_5", new CubeData { color = "orange", size = 0.7f, position = 5 } },
            { "Cube_1_6", new CubeData { color = "opaque", size = 0.6f, position = 6 } },
            { "Cube_1_7", new CubeData { color = "orange", size = 0.5f, position = 7 } },
            { "Cube_1_8", new CubeData { color = "orange", size = 0.2f, position = 8 } },
            { "Cube_2_1", new CubeData { color = "opaque", size = 0.2f, position = 9 } },
            { "Cube_2_2", new CubeData { color = "orange", size = 0.5f, position = 10 } },
            { "Cube_2_3", new CubeData { color = "opaque", size = 0.7f, position = 11 } },
            { "Cube_2_4", new CubeData { color = "orange", size = 0.6f, position = 12 } },
            { "Cube_2_5", new CubeData { color = "orange", size = 0.3f, position = 13 } },
            { "Cube_2_6", new CubeData { color = "orange", size = 0.4f, position = 14 } },
            { "Cube_2_7", new CubeData { color = "opaque", size = 0.5f, position = 15 } },
            { "Cube_2_8", new CubeData { color = "opaque", size = 0.8f, position = 16 } }
        };
#

Then to "swap" I am running :

(note cubeSelected is the gameObject/cube they selected first)

int cubeSelectedPosition = cubeState[cubeSelected.name].position;
int cubePosition = cubeState[cube.name].position;
cubeState[cube.name] = new CubeData { color = cubeState[cube.name].color, size = cubeState[cube.name].size, position = cubeSelectedPosition };
cubeState[cubeSelected.name] = new CubeData { color = cubeState[cubeSelected.name].color, size = cubeState[cubeSelected.name].size, position = cubePosition }; ;


However when, immediately after, I run

          foreach (var (name, data) in cubeState)
          {
              Debug.Log($"Name: {name}, Color: {data.color}, Size: {data.size}, Position: {data.position}");
          }

The position values did not change. Any ideas / I'm lost.

echo bridge
#

This looks like it should work. I would check that cubeSelected and cube aren't the same object.

tulip idol
#

You got it. Its my first time updating Dictionaries and I missed the obvious. Thanks so much for your help!

echo bridge
#

ha, nice (:

#

no problem!