I am working on creating a list of wall coordinates for my game and when I attempt to add the new wall item to the list of walls and it always overwrites every value in the list. Can someone look at my function and figure out what I’m doing wrong, please.

public void Start()
{
wallList = new List ();
}

``````public void Start()
{
wallList = new List<WallItem> ();
}

{
//make a temporary wall item
WallItem tempWallLeft = new WallItem();
WallItem tempWallRight = new WallItem ();

//add more walls to get below the bottom y
while (wallY < c_wallBottomY)
{
//drop down to the next row
wallY += wallHeight;

//vary the x to give the cave wall some texture and challenge
float step = c_wallMinXStep + Random.Range (c_wallMinXStep, c_wallMaxXStep);
float r = Random.Range (0, 2);
if(r == 0)
{
step *= -1;
}
wallX += step;
wallX = Mathf.Max (c_wallXMargin + wallDistance/2, Mathf.Min (wallX, Screen.width - c_wallXMargin - wallDistance/2));

tempWallLeft.x = wallX - wallDistance/2;
tempWallLeft.y = wallY;
//Debug code
//Debug.Log ("wallX is " + wallX + " wallY is " + wallY);
Debug.Log ("tempWallLeft.x = " + tempWallLeft.x + " tempWallLeft.y = " + tempWallLeft.y);

tempWallRight.x = wallX + wallDistance/2;
tempWallRight.y = wallY;

//debug code to count # of walls
//Debug.Log ("wallY is " + wallY);
Debug.Log ("tempWallRight.x is " + tempWallRight.x + " tempWallRight.y is " + tempWallRight.y);
//Debug.Log ("numWalls is " + numWalls);
//Debug.Log ("wallHeight is " + wallHeight);

//more debug code
for (int i = 0; i < wallList.Count; i++)
{
Debug.Log ("wallList[" + i + "].x = " + wallList_.x + " wallList[" + i + "].y = " + wallList*.y);*_
``````

* }*
}
}

Looks like you are adding a reference to temp wall Into the list and so when you change the values, all the wall items change because they are all referencing the same wall item. Try this, make a constructor for wall item that takes the x and y and change

``````wallList.Add(tempWallLeft)

// change to

``````

For both left and right wall items

EDIT: duh never mind you’re only making one wall item for left and right, move the leftWallItem = new wallItem parts into your while loop

@Slobdell was on the right track there. The reason you are having this issue is because you are creating temporary WallItem’s outside of your loop and just changing the x and y values each time the loop happens.

When you use a List and Add a WallItem, it is not adding a WallItem copy, but instead a reference to the copy. So your logic is essentially doing:

``````Create WallItem A
Create WallItem B
Loop 1
Set WallItem A.x & .y
Set WallItem B.x & .y
Loop 2
Set WallItem A.x & .y
Set WallItem B.x & .y
``````

So what you need to do is change your code to ensure that each WallItem that is being added to the List is a new WallItem instance. By moving your WallItem constructors to inside the loop it will ensure this happens and fix your issue. See below for this code:

``````private void AddWalls()
{
//add more walls to get below the bottom y
while (wallY < c_wallBottomY)
{
//make a temporary wall item
WallItem tempWallLeft = new WallItem();
WallItem tempWallRight = new WallItem();

//drop down to the next row
wallY += wallHeight;
``````