How can i Instantiate objects of each object from the array ?

In the array objectsToInstantiate for example it’s size is 3.
In index 0 i have a cube in index 1 cylinder and index 2 sphere.
And in the variable numberOfObjectsToCreate the value is 20.

So what i want to do is to Instantiate 20 clones of the cube then 20 clones of the cylinder and 20 clones of the sphere.

But something is wrong with the for loop in the generateObjectOnTerrain()
It’s not creating 20 clones of each object. It’s creating only 3 one of each object.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//[ExecuteInEditMode]
public class InstantiateObjects : MonoBehaviour
{
    public GameObject[] objectsToInstantiate;
    public Terrain terrain;
    public float yOffset = 0.5f;
    public int numberOfObjectsToCreate;
    public bool parent = true;
    public bool randomScale = false;
    public float setRandScaleXMin, setRandScaleXMax;
    public float setTandScaleYMin, setTandScaleYMax;
    public float setTandScaleZMin, setRandScaleZMax;

    private float terrainWidth;
    private float terrainLength;
    private float xTerrainPos;
    private float zTerrainPos;
    private GameObject objInstance;
    private GameObject[] createdObjects;
    private string objname;

    public void Start()
    {
        //Get terrain size
        terrainWidth = terrain.terrainData.size.x;
        terrainLength = terrain.terrainData.size.z;

        //Get terrain position
        xTerrainPos = terrain.transform.position.x;
        zTerrainPos = terrain.transform.position.z;

        for(int i = 0; i < objectsToInstantiate.Length; i++)
        {
            objname = objectsToInstantiate*.name;*

MyCustomEditor.TagsAndLayers.AddTag(objname);
}

generateObjectOnTerrain();
}

public void Update()
{

}

public void DestroyObjects()
{
UpdateList(true);

if (createdObjects != null && createdObjects.Length > 0)
{
for (int i = 0; i < createdObjects.Length; i++)
{
DestroyImmediate(createdObjects*);*
}
createdObjects = new GameObject[0];
}
}

public void generateObjectOnTerrain()
{
for (int i = 0; i < numberOfObjectsToCreate; i++)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);

float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));

//Generate random x,y,z scale on the terrain
float randScaleX = Random.Range(setRandScaleXMin, setRandScaleXMax);
float randScaleY = Random.Range(setTandScaleYMin, setTandScaleYMax);
float randScaleZ = Random.Range(setTandScaleYMax, setRandScaleZMax);

//Apply Offset if needed
yVal = yVal + yOffset;

//Generate the Prefab on the generated position
objInstance = Instantiate(objectsToInstantiate*, new Vector3(randX, yVal, randZ), Quaternion.identity);*

if (randomScale == true)
objInstance.transform.localScale = new Vector3(randScaleX, randScaleY, randScaleZ);

if (parent)
objInstance.transform.parent = this.transform;

objInstance.tag = objname;
}

createdObjects = GameObject.FindGameObjectsWithTag(objname);

if (objname == “Teleportation Booth”)
UpdateList(false);
}

private void UpdateList(bool destroy)
{
GameObject go = GameObject.Find(“Main Camera”);
var list = go.GetComponent().Targets;
if (destroy == false)
{
list.AddRange(createdObjects);
go.GetComponent().Targets = list;
}

if (destroy == true)
{
list.Clear();
go.GetComponent().Targets.Clear();
}
}
}

Maybe:

 {
     public GameObject[] objectsToInstantiate;
     public Terrain terrain;
     public float yOffset = 0.5f;
     public int numberOfObjectsToCreate;
     public bool parent = true;
     public bool randomScale = false;
     public float setRandScaleXMin, setRandScaleXMax;
     public float setTandScaleYMin, setTandScaleYMax;
     public float setTandScaleZMin, setRandScaleZMax;
 
 private float terrainWidth;
 private float terrainLength;
 private float xTerrainPos;
 private float zTerrainPos;
 private GameObject objInstance;
 private GameObject[] createdObjects;
 private string objname;

 public void Start()
 {
     //Get terrain size
     terrainWidth = terrain.terrainData.size.x;
     terrainLength = terrain.terrainData.size.z;

     //Get terrain position
     xTerrainPos = terrain.transform.position.x;
     zTerrainPos = terrain.transform.position.z;

     for(int i = 0; i < objectsToInstantiate.Length; i++)
     {
         objname = objectsToInstantiate*.name;*

MyCustomEditor.TagsAndLayers.AddTag(objname);
generateObjectOnTerrain(objectsToInstantiate*);*
}

}

public void Update()
{

}

public void DestroyObjects()
{
UpdateList(true);

if (createdObjects != null && createdObjects.Length > 0)
{
for (int i = 0; i < createdObjects.Length; i++)
{
DestroyImmediate(createdObjects*);*
}
createdObjects = new GameObject[0];
}
}

public void generateObjectOnTerrain(GameObject objectToInstantiate)
{
for (int i = 0; i < numberOfObjectsToCreate; i++)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);

float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));

//Generate random x,y,z scale on the terrain
float randScaleX = Random.Range(setRandScaleXMin, setRandScaleXMax);
float randScaleY = Random.Range(setTandScaleYMin, setTandScaleYMax);
float randScaleZ = Random.Range(setTandScaleYMax, setRandScaleZMax);

//Apply Offset if needed
yVal = yVal + yOffset;

//Generate the Prefab on the generated position
objInstance = Instantiate(objectToInstantiate, new Vector3(randX, yVal, randZ), Quaternion.identity);

if (randomScale == true)
objInstance.transform.localScale = new Vector3(randScaleX, randScaleY, randScaleZ);

if (parent)
objInstance.transform.parent = this.transform;

objInstance.tag = objectToInstantiate.name;
}

createdObjects = GameObject.FindGameObjectsWithTag(objectToInstantiate.name);

if (objectToInstantiate.name == “Teleportation Booth”)
UpdateList(false);
}

private void UpdateList(bool destroy)
{
GameObject go = GameObject.Find(“Main Camera”);
var list = go.GetComponent().Targets;
if (destroy == false)
{
list.AddRange(createdObjects);
go.GetComponent().Targets = list;
}

if (destroy == true)
{
list.Clear();
go.GetComponent().Targets.Clear();
}
}
}