- Home /
List values change for no apparent reason
Hi there, this is my first post on here, so sorry if I do not follow a proper format or something.
Essentially, I have two lists of GameObjects. Both lists are the same length. One list is a reference and the other list (let's call this the normal list) are those same elements but in a different order. What I want to do is to match the positions of each element in the normal list to the position of the same-indexed element in the reference list.
What I have written already works perfectly except for two GameObjects. For some reason in my first and second loop of the for-loop, two positions in my reference list just change for no reason that I can tell.
Here is my code:
private void rotate(List<GameObject> cubeList)
{
//make a copy of cubeList to reference
List<GameObject> referenceList = copyList(cubeList);
//change the cube list
//get temporary values of the first corner and first middle
GameObject tempCorner = cubeList[cubeList.Count - 2];
GameObject tempMiddle = cubeList[cubeList.Count - 1];
//move the corners and middles accordingly in the list
for(int i = cubeList.Count - 1; i > 1; i--)
{
cubeList[i] = cubeList[i - 2];
}
//change the first and second cube spots
cubeList[0] = tempCorner;
cubeList[1] = tempMiddle;
//actually move the cubes now by making the cube list position equal to the reference list position
for(int i = 0; i < cubeList.Count; i++)
{
cubeList[i].transform.position = referenceList[i].transform.position;
cubeList[i].transform.Rotate(new Vector3(0, 0, -90));
}
}
private List<GameObject> copyList(List<GameObject> list)
{
List<GameObject> copyList = new List<GameObject>();
for(int i = 0; i < list.Count; i++)
{
copyList.Add(list[i]);
}
return copyList;
}
Here are some of my debugger statements, so you can see what I am talking about.
Proper Reference List:
(-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, -1.0, -1.0), (-1.0, 0.0, -1.0),
Pass 0:
Reference List:
(-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, 1.0, -1.0), (-1.0, 0.0, -1.0),
Cube List:
(-1.0, 1.0, -1.0), (-1.0, 0.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0),
Pass 1:
Reference List:
(-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0),
Cube List:
(-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0),
Pass 2:
Reference List:
(1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0),
Cube List:
(-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0),
Pass 3:
Reference List:
(1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0), (-1.0, 1.0, -1.0), (0.0, 1.0, -1.0),
Cube List:
(-1.0, 1.0, -1.0), (0.0, 1.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, 1.0, -1.0), (1.0, 0.0, -1.0), (1.0, -1.0, -1.0), (0.0, -1.0, -1.0),
and so forth.
Pass 0 and Pass 1 are the only things that don't work (look at the last two positions, they change on the reference list when they shouldn't).
I am completely stumped. I have been trying other ways, really all I can think of, but referenceList continues to be altered. It is baffling me. Any help would be appreciatedQ
Your code is a little convoluted. How about describing what is it you are trying to do?
Can you writer an example and a use case for your code please?
Answer by UnderShad · Dec 03, 2020 at 09:17 PM
Try using List<GameObject> referenceList = new List<GameObject>(cubeList);
instead of copyList . For me list are notoriously using reference instead of copy.
Hey! So unfortunately that didn't work. Any other suggestions? Any idea of how my reference list is being altered? I appreciate your help!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Why is my list empty? (c# question) 1 Answer
Check if Exists and if so, Add it to Other List 1 Answer
Why is my list not adding? 1 Answer