Insantiating randomly within a collider.

Hi Unity community!

What I’m trying to find out is this; how do I insantiate trees within a collider/boundary. Just one or two, every 10 seconds or so. Here is a picture.

The island itself is not a terrain but rather a plane. I have, however, set up an invisible terrain on top, the exact same size (although I cannot think of a reason to use this).

TLDR; how do I insantiate a prefab every 10 seconds within a collider GameObject or some boundary of some sort.

Thanks!

Here is rough example, it should be enough for understanding, but be sure that you have only 1 instance of this script in whole gameworld:

[SerializeField]
private GameObject treeObj;
private const float halfWidth = 300f;
private const float halfHeight = 300f;
private const int maxTreePerVawe = 2f;
[SerializeField]
private float waveWait = 10f;
private void Start()
{
    StartCoroutine(SpawnWaves());
}
    private void SpawnTree(int count)
{
    for (int i=0; i < count; i++)
    {
        // considering that Y:0 is the flat position of your plane
        Vector3 spawnPosition = new Vector3(UnityEngine.Random.Range(-halfWidth, halfWidth), 0f, UnityEngine.Random.Range(-halfHeight, halfHeight)); // you can get those values from "plane's "width/height"" if you want instead of adding them in script
        spawnPosition += transform.position; // where transform.position is basically the dot in the world which represents the center of your "plane", can be removed if it is 0:0:0, or changed if script is not attached to "plane"
        GameObject newTree = Instantiate(treeObj, spawnPosition, Quaternion.identity) as GameObject;
        // do here with newTree anything else that you want to do on initiation, like customizing Randomly size for example
    }
}
IEnumerator SpawnWaves()
{
    yield return new WaitForSeconds(waveWait);//initial wait
    while (playerModel.isAlive)
    {
        SpawnTree(UnityEngine.Random.Range(1, maxTreePerVawe));
        yield return new WaitForSeconds(waveWait);//continue loop wait while player is alive or some other condition
    }
}