Obtain a list of values ​​from the dictionary

advertisements

I'm beginner in c#, and I'm trying to get a list of values but I get a null object reference.

I have a dictionary

var stripes = new Dictionary<int, List<Transform>>();

and I'm trying to get the list of transforms

       foreach (var stripe in stripes)
            {
                List<Transform> transforms = stripe.Value;
                foreach (var treeTransform in transforms)
                {
                    float distance = DistanceToCameraPlane(treeTransform.position);
                    TreeObject treeObject = new TreeObject();
                    treeObject.distance = distance;
                    treeObject.tree = treeTransform;
                    sortedTrees.Add(treeObject);
                }
            }

However I get a null object references in treeTransform.position


Try to change this line:

foreach (var stripe in stripes)

to this instead:

foreach (List<Transform> transforms in stripes.Values)

and remove this:

List<Transform> transforms = stripe.Value;