Problem with list, trying to find something by coordinates in Vector2

Hello. So I made a list from Lands.cs, which populates with “imaginary” coordinates depending on terrain size. For example my terrain is size of 100. It makes 100 tiles with coordinates in Vector3, 1,1 , 0,1, etc.
So when my list is populated, I got a script depending on my mouse location, which gets the tile’s x and z(y in vector2 saved in list), and I am trying to find which tile is it (each tile has a different id) depending on those two coordinates in vector2. I am not sure how to use lists or linq for it, after I tried few things such as Find, Contains, Where, but I am not sure on how to do it.

My Lands.cs

[System.Serializable]
public class Lands {

    public int landID;
    public Vector2 tile;
    public int ownerID;

    public Lands(int id, Vector2 tile, int oID)
    {
        this.landID = id;
        this.tile = tile;
        this.ownerID = oID;
    }
}

Scripts that finds the tile part:

if (GetComponent<Collider>().Raycast(ray, out hitInfo, Mathf.Infinity))
        {
            x = Mathf.FloorToInt(hitInfo.point.x / terrainTileSize);
            z = Mathf.FloorToInt(hitInfo.point.z / terrainTileSize);
            Debug.Log ("Tile: " + x + ", " + z);

            currentTileCoord.x = x;
            currentTileCoord.z = z;
         // cube which displays which tile I am hovering on, imaginary
        selectionCube.transform.position = currentTileCoord * terrainTileSize + new Vector3(terrainTileSize / 2 ,0 , terrainTileSize / 2);

         // find tile in list depending on x and z coord, saved in list, made from Lands.cs as tile as Vector2

Please edit your post using the code tags - Using code tags properly - Unity Engine - Unity Discussions

Something like this will find a Lands object in the list with a tile that matches your X and Z values:

Lands land = landsList.Find(landsObject => landsObject.tile.x == x && landsObject.tile.y == z);

Just make sure to null check it just in case it doesn’t find it for some reason.

You may want to consider a 2D array so you can look up things like “lands[x, z]”, as is common in grid-based systems.

If you’re using == on anything float-based, you run the risk of stumbling across floating point precision issues.

The second suggestion here is much more valuable: Use a 2D array (probably static) to find the tile. This will be more reliable and much faster (especially if you add more tiles later on)

 //in whatever creates your tiles, let's say TileSpawner.cs
public static Lands[,] allTiles;
public int rows = 10;
public int cols = 10;

void Awake() {
allTiles = new Lands[cols, rows];
for (int x=0; x<cols; x++) {
for (int y=0; y<rows; y++) {
GameObject thisTileGO = Instantiate(whatever);
allTiles[x,y] = thisTileGO.GetComponent<Lands>();
}
}
}

//elsewhere
if (GetComponent<Collider>().Raycast(ray, out hitInfo, Mathf.Infinity))
{
int x = Mathf.FloorToInt(hitInfo.point.x / terrainTileSize);
int z = Mathf.FloorToInt(hitInfo.point.z / terrainTileSize);
Debug.Log ("Tile: " + x + ", " + z);
Lands thisTile = TileSpawner.allTiles[x, z];
}

edit: I assumed that Lands was a MonoBehaviour in this that’d be attached to the tiles. Well hope this helps you figure out how to use 2D arrays, anyway.

1 Like