I’m working on a simple village building game in Unity3D, C#. I made it so the game would generate a 50x50 block of ground that is made up of clones of a prefab. These clones are children of a gameobject called Ground. They are called groundChild in the script, and the parent of those children is called groundParent in the script. My script currently:
//GameObjects
public GameObject cube;
public GameObject groundParent;
private GameObject forestObject;
//Integers
public int worldWidthX;
public int worldWidthZ;
public int mapHeight;
//Floats
private float mapSizeX;
private float mapSizeZ;
private float forestMaxAmount;
private float forestMinAmount;
private float forestAmount;
private float forestObjectNumber;
public List<Transform> forestObjects = new List<Transform> ();
public void GenerateTerrain()
{
for(int x = 0; x <= this.worldWidthX; x++, x++)
{
for(int z = 0; z <= this.worldWidthZ; z++, z++)
{
float y = /*Mathf.PerlinNoise(x / 30, 76) * Mathf.PerlinNoise(z / 30, 22) * 40;*/ mapHeight;
GameObject groundChild = Instantiate(this.cube, new Vector3(x, y, z), this.cube.transform.rotation);
groundChild.transform.parent = groundParent.transform;
}
}
forestMaxAmount = (worldWidthX * worldWidthZ) / 8;
forestMinAmount = (worldWidthX * worldWidthZ) / 25;
forestAmount = Mathf.RoundToInt(Random.Range (forestMinAmount, forestMaxAmount));
Debug.Log (forestAmount);
for (var i = 0; i <= forestAmount; i++)
{
forestObjectNumber = Mathf.RoundToInt(Random.Range (forestMinAmount, forestMaxAmount));
}
/*foreach (GameObject child in groundParent.transform)
{
if (child.tag == "Forest")
{
//Add Trees
}
}*/
}
What I’m having trouble figuring out is how to correctly and randomly choose out of these children gameobjects and then apply a tag change to them.