Hello All! Unity newbie here, I could use some help with a script I am making. I am working on generating a visible grid to better help me identify where the actual tiles are for my map. I have this very simple method:
// Generates grid for the map
void GenGrid()
{
// Initialize grid tiles array
gridSprites = new GameObject[iMapWidth, iMapHeight];
// Traverse tile rows
for (int x = 0; x < iMapWidth; x++)
{
for (int y = 0; y < iMapHeight; y++)
{
// Instantiate a grid tile sprite at this position on the map
gridSprites[x, y] = (GameObject)Instantiate(Resources.Load(“GridTile”) );
gridSprites[x, y].transform.position = new Vector3((x * iTileWidth) / 100, (y * iTileHeight) / 100, 0);
}
}
}
All works fine up until I try to position the game object. See, my prefab has a default position of 0, 0, 0. So all of the clones get generated at 0, 0, 0. And obviously I want to line them up next to each to form a grid. But even though I am setting position, they in game they are still located at 0, 0, 0. Why is this?
Are iTileWidth and iTileHeight integers? What are their values?
int x = 2;
int iTileWidth = 30;
// This line will print "(x * iTileWidth) / 100 is equal to: 0"
Debug.Log("(x * iTileWidth) / 100 is equal to: " + ((x * iTileWidth) / 100));
EDIT: Try dividing by 100f, instead of 100 if this is the case.
Thank you! Issue resolved. iTileWidth and iTileHeight were integers. Position was being divided by 100 to turn from whole number to decimal. So instead of a position of x:32 I get x: 0.32, which is where the tile actually needs to be.
// Traverse tile rows
for (int x = 0; x < iMapWidth; x++)
{
// Traverse tile columns
for (int y = 0; y < iMapHeight; y++)
{
// Instantiate a grid tile sprite at this position on the map
gridSprites[x, y] = (GameObject)Instantiate(
Resources.Load(“GridTile”),
new Vector3((x * iTileWidth) / 100f, (y * iTileHeight) / 100f, 0),
Quaternion.identity);
// Set the grid sprite as a child of the map
gridSprites[x, y].transform.parent = gameObject.transform;
// Set the grid sprite name
gridSprites[x, y].name = “Grid - X:” + x + " Y:" + y;
}
}
}