adding an element to array list causes all item in that array list to be set to the new item

so this one has been racking my brain for a while, for some reason adding an element to an array list is causing all items in that array list to equal the new item.

public void addNav(int x, int y)
		for(int i = 0; i < playerMap.Count; i++)
		{//this outputs the current list of nav points so far
			queue z = (queue)playerMap*;* 

//queue is just a class with int x,y symbolizing the location of the a tile in the game

  •  	Debug.Log ("list so far i = " + i);*
  •  	Debug.Log ("add nav X zx = " + z.x);*
  •  	Debug.Log ("add nav X zy = " + z.y);*
  •  }*
  •  Debug.Log("current nav x = " + x); //the x passed into the function*
  •  Debug.Log("current nav y = " + y); //the y passed into the function*
  •  tempMap.x = x;*
  •  tempMap.y = y;*
  •  playerMap.Add (tempMap); //adding to the array list*
  •  int count = playerMap.Count;*
  •  Debug.Log("checking");*
  •  for(int i = 0; i < playerMap.Count; i++)*
  •  {*

_ queue z = (queue)playerMap*;_
Debug.Log ("checking list = " + i);_
Debug.Log ("checking add nav X zx = " + z.x);_
Debug.Log ("checking add nav X zy = " + z.y);_
}//and this prints out the list at the end of the function*_

* }*

and here is the output (sorry to type it out butt…)
current nav x = 48 (keep in mind the first run through the count is 0 so the first debug is skipped)
current nav y = 15
checking list = 0
checking add nav x zx = 48
checking add nav x zy = 15
so hurray so far, then we go to the second run through;
list so far i = 0
add nav x zx = 48
add nav x zy = 15
current nav x = 48
current nav y = 14
checking list = 0
checking add nav x zx = 48
checking add nav x zy = 14
checking list = 1
cehcking add nav x zx = 48
checking add nav x zy = 14
and this continues for every element of the array that we add

relevant declarations

public class theMap
* public int x;*
* public int y;*
public ArrayList playerMap = new ArrayList();
any thoughts on why this is occurring will be appreciated.

however you did make me think of something, every run the last item in the list is correct until a new item is added, it made me think the element was pointing to a memory location of playermaps. looking through my code I remembered this function map.findPath (myPlayer, ref enamy); and then in find path enamy.addNav(i, j); I wonder, I wonder could it be that passing it by reference causes all sub variables to be looked at by reference? adjusting the code to

queue tempmaps = new queue();

	tempmaps.x = x;

	tempmaps.y = y;

	playerMap.Add (tempmaps);

fix’s the issue.

The problem is your tempmap variable.

you set its x and its y then you add it to the array list, so the array list has a pointer to the variable.

When you set its x and its y again you are changing the values of the pointer variable then adding another reference to the same object so both items in the array are pointing to the same object which now has the new values.

You either need to new up the object or create a deep copy of that object so the system assigns it a new pointer.

so just do:

tempMap = new queue();
tempMap.x = x;
tempMap.y = y;
playerMap.Add (tempMap);

or simply

playerMap.Add (new queue() { x=x, y=y});