Weird grid naming bug.

I have been trying to create a grid in Unity by using a plane prefab that is 100x1x100 in size. I am trying to name the various tiles depending on their x and z values, but for some reason it does not work correctly. The tile at position X = 0 , Z = 0 is always named 19,19 and not 0,0. The tile next to it does get the name 0,0, but that one should be 0,1. Is there something weird in my code?

function Start () {

	for(x=0; x<20; x++)
	{
		for(z=0; z<20; z++)
		{
		var tilePosition : Vector3;
		tilePosition = Vector3(x * 100, -0.5f , z * 100);
		Instantiate(floorTile, tilePosition, Quaternion.identity);
		floorTile.name = x + "," + z;
		}
	}

}

i assume floorTile is the gameobject/prefab which is assigned in the editor. as you rename it all spawned instances also get this name. store the objectreference which is returned by instantiate and change its name.

    function Start () {
     
        for(x=0; x<20; x++)
        {
            for(z=0; z<20; z++)
            {
            var tilePosition : Vector3;
            tilePosition = Vector3(x * 100, -0.5f , z * 100);
            GameObject tmpobj = Instantiate(floorTile, tilePosition, Quaternion.identity);
            tmpobj.name = x + "," + z;
            }
        }
     
    }

also its advisable to format the namestring with leading zeros to avoid confusion. something like string.Format(“{0:000},{1:000}”, x,z);

also consider that creating large grids (ala 100x100) upwards creates lots of gameobjects which can cause performance issues especially on mobile. if you want to create some kind of terrain try a better approach or join the meshes.

Thanks, I see what I did wrong. I’m not sure what you mean with leading zero’s though. Do you mean that every tile should be named with two decimals? Like: (02 , 05)?

The reason I am using planes, is because I want them to be clickable and lift up when you do. 20x20 is about the biggest I want to go. Is it fine to keep this configuration for that? Thanks again.

yes, this is especially for sorting as string sort usually sort 1,10, … 2, 20,

then they need a collider. also note that the standard unity builtin planes do not consist of 4 vertices but of 100 or something. you may consider importing your own plane from a 3d modeler of your choice.

i think so.