Hello, I need to make a grid for a rts game im making. It’s going to be used in the making of the buildings. And what i’m going to do is basically draw a 68x68 grid and then i can select them by clicking on them and then i can hit the create button and it will place the selected building. But my problem is i can create a 1x68 grid but i don’t know how to create the rest. If i put it in a for loop then it will only create a 1x1 grid and then crash as far as i know xD
My code is:
var Prefab : Transform;
var forValueX : float;
var XPos : float;
var YPos : float;
var ZPos : float;
function Start(){
XPos = 1.829174;
YPos = -105.606;
ZPos = 1.927386;
for (forValueX = 0; forValueX <= 68; forValueX++) {
Debug.Log("Value of X is: " + forValueX);
Instantiate(Prefab, new Vector3(XPos,YPos,ZPos), Quaternion.identity);
XPos += 1.829174;
XPos += 1.8;
}
}
function Update()
{
}
If you can see the problem i’m encountering and can help me then thanks so much!! 
All of this code is untested.
Rect[,] grid = new Rect[68,68];
float rectSize = 1.0f;
//68 can be replaced by the length of the
//This will initalize a grid of rectangles which you
//can use to "hittest" towards
void Start()
{
for(int y = 0; y < grid.getLength(1); y++)
{
for(int x = 0; x < grid.getLength(0); x++)
{
grid[x,y] = new Rect(x * rectSize, y * rectSize, rectSize, rectSize);
}
}
}
void Update()
{
//You need to spawn a plane with a size similar to your grid which also fits
//Inside your fully sized grid
//And use that plane to get the click position if the mouse button is clicked.
if(Input.GetMouseButtonDown(0))
{
//Cast a ray from the camera to the "grid plane", store the ray result
//In a RaycastHit
//Get the "mouse position" from the RaycastHit.point
Vector2 mpos = new Vector2(
someRaycastHit.point.x,
someRaycastHit.point.z
);
Rect slot = GetClickedRect(mpos);
//Spawn whatever building or such in the center of the rectangle here
Debug.Log(slot);
}
}
Rect GetClickedRect(Vector2 pos)
{
foreach(Rect r in grid)
{
if(r.Contains(pos))
{
return r;//found slot skip the rest of the foreach loop
}
}
//If the code ever gets here the click was out of bounds
return null;
}