I’m attempting to spawn a gameobject along the x axis of my grid in random locations (0-7) along the 7th row of the y axis. I’ve been racking my brain for several days trying to get this to work and I have been unsuccessful. I’m looking for help on how to solve this problem. Here is my code for my grid.
public GameObject gridPiece;
public int Width = 10;
public int Height = 10;
//private GameObject [,] grid = new GameObject[10,10]; Didn't contribute to array code.
void Awake ()
{
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{GameObject grid = (GameObject)Instantiate(gridPiece);
grid.transform.position = new Vector2(gridPiece.transform.position.x +x,
gridPiece.transform.position.y +y);
grid.transform.parent = gameObject.transform;
}
}
}
Here is the code I have for my spawner.
//Random Block Spawner
void SpawnRandomObject(){
int whichItem = Random.Range (0, 4);
GameObject myblock = Instantiate (enemySet1[whichItem]) as GameObject;
myblock.transform.position = transform.position;
}