Hi! I’ve been reading up on loops, iterating, and removing items from lists, but I’m still rather muddled in terms of my own project.
Project Info: [see pictures below] A 2D grid that is instantiated through code. The edge tiles are located and sent to a List. From the list, a random Tile is selected and an Entrance Tile is placed. At the same time, information on every tile’s neighbors is sent to each Tile. If the Tile recognizes itself as an Entrance Tile, it returns the neighbor tile information back to the Board script. Then, it removes all of its neighboring tiles from the edge tile List. The idea is that after removing these neighboring tiles, the Exit Tile triggers a second randomized selection from the new edgeList and then places itself on the board. Then the board populates with other random Tiles.
The problem: When the edge tile looks for a randomized Tile, the edgeTiles List it’s using is not updated.
The edge tile total is 24. Minus the entrance tile, 23. Minus its neighbors, the total edgeTile List should be somewhere between 19-22. For some reason, the edge tile debug log I run is returning 23.
However, when I run the script and look at the inspector, it is clear that the Entrance Tile is sending and receiving information on its neighbors and is in fact removing them from the edgeTile List. (See pictures below).
I figure I must be calling the SetExit at the wrong time or something. Here’s my code.
#region setting neighbors
public void SetNeighbors() [...]
public void CallingNeighbors(List<GameObject> neighbors)
{
foreach (var x in neighbors)
{
neighborTiles.Add(x);
if (edgeTiles.Contains(x))
{
edgeTiles.Remove(x); [B][is this the problem?][/B]
}
}
}
#endregion
#region set entrance/exit
public GameObject ChooseRandomNumber(List<GameObject> tileRandomize)
{
int randomNum = Random.Range(0, tileRandomize.Count);
GameObject randomTile = tileRandomize[randomNum];
return randomTile;
}
public void SetEntrance()
{
GameObject entranceTile = ChooseRandomNumber(edgeTiles);
entranceTile.GetComponent<SpriteRenderer>().color = entranceColor;
entranceTile.GetComponent<SpriteRenderer>().sprite = entranceSprite;
entranceTile.name = new string("Entrance");
edgeTiles.Remove(entranceTile);
//entranceTile.GetComponent<scriptTile>().isTileFilled = true;
entranceTile.GetComponent<scriptTile>().SetFilledStatus(true);
SetExit();
}
public void SetExit()
{
Debug.Log("edgetile Amt: " + edgeTiles.Count); [B][this is returning as 23 every time!][/B]
GameObject exitTile = ChooseRandomNumber(edgeTiles);
exitTile.GetComponent<SpriteRenderer>().color = exitColor;
exitTile.GetComponent<SpriteRenderer>().sprite = exitSprite;
exitTile.name = new string("Exit");
edgeTiles.Remove(exitTile);
//exitTile.GetComponent<scriptTile>().isTileFilled = true;
exitTile.GetComponent<scriptTile>().SetFilledStatus(true);
isEntranceExitSet = true;
}
#endregion
Am I calling SetExit at the wrong time or in the wrong place? Or is the edgeTile.Remove not changing across the board?
Things I’ve tried:
-
moving SetExit around (so far all the spots I’ve chosen haven’t fixed it or just wouldn’t call SetExit properly)
-
attempting to populate a second list from the “new” edgeTile list and calling on that (threw an array exception for some reason?)
-
making a new “ChooseRandomNumber” method (didn’t change anything)
-
limiting the trigger of SetExit to specific time with a bool (resulted in SetExit just not happening)
Any advice is welcome! Thank you!



