how to make random object in mesh

greatings ! My mainly problem is i create a [island][1]** to my game. and I wanted to randomly do tree etc. to this system. but the code is spawned flat. I want it to be spawned separately, but specifically for the mesh.

the code i mentioned

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

public class TerrainWorldGenerator : MonoBehaviour
{
    public GameObject obj;
    public GameObject treeinworldobj;
    public int objamount;
    List<GameObject> objlist = new List<GameObject>();
    GameObject[] objArray;

    private void Start() {
        for(int i = 0; i <= objamount; i++){
            objlist.Add(Instantiate<GameObject>(obj));
            objArray = objlist.ToArray();
            objArray*.transform.position = new Vector3(Random.Range(-50, 50), 0, Random.Range(-50, 50));*

objArray*.transform.parent = treeinworldobj.transform;*
}
}
}
[what i want][2] **
[what is happening now][3] **
(Click on the text to access the picture) **
[1]: Procedural Terrain Generation - YouTube
_[2]: https://imgur.com/SsmwP81*_
_
[3]: https://imgur.com/a/xDWaJJ8*_

Do you have a mesh collider on the islands mesh? If yes you could just raycast down, towards the terrain, to find the correct height for each tree. Something like this:

private void Start() {
         for(int i = 0; i <= objamount; i++){
             objlist.Add(Instantiate<GameObject>(obj));
             objArray = objlist.ToArray();

             Vector3 treePosition = new Vector3(Random.Range(-50, 50), 10000, Random.Range(-50, 50));

             if(Physics.Raycast(treePosition, Vector3.down, out RaycastHit hitInfo))
             {
                      treePosition = new Vector3(treePosition.x, hitInfo.point.y, treePosition.z);
             }

             objArray*.transform.position = treePosition;*

objArray*.transform.parent = treeinworldobj.transform;*
}
}
I have not tested the code, but it should work if you do something like that :smiley:
Hope it helps

_*https://docs.unity3d.com/ScriptReference/Physics.Raycast.html*_
_*https://docs.unity3d.com/ScriptReference/RaycastHit.html*_