Question by
GibletsofJesus · Nov 06, 2015 at 10:39 PM ·
c#listslist of lists
Problem adding List<> objects to another List<>
Hey all,
Currently I've got a two lists for vector3's and quaternions and I'm adding them to a list of these lists so I can keep record of object positions each time the function is called. Everything seems to work largely as intended, except it seems that if I replace a list with a new set of data (after passing it into the list of lists), the original data that got added into the list of lists is also changed.
Is there a way of just adding the data from a list to another array type variable without having all the values reset?
<Vector3> pos = new List<Vector3>();
List<Quaternion> rot = new List<Quaternion>();
List<List<Vector3>> posArray = new List<List<Vector3>>();
List<List<Quaternion>> rotArray = new List<List<Quaternion>>();
void updateVariables()
{
pos.Clear();
rot.Clear();
for (int i = 0; i < boxes.Length; i++)
{
pos.Add(boxes[i].transform.position);
rot.Add(boxes[i].transform.rotation);
}
posArray.Add(pos);
rotArray.Add(rot);
}
Comment
Best Answer
Answer by GibletsofJesus · Nov 06, 2015 at 08:03 PM
PROBLEM SOLVED!
Replacing this line
posArray.Add(pos);
With
posArray.Add(new List<Vector3>(pos));
Seems to fix it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612044152im_/https://answers.unity.com/themes/thub/images/avi.jpg)