Raycast hit detection with MeshCollider

Hello,

I am creating an application that has to do with digital twin inspection. I am working with a ZED camera to get spatial mapping data saved to an .obj file. I am then instantiating a GameObject at runtime using this file in a new scene. I want the user to be able to click on an area of interest on the GameObject and get precise feedback on where the mesh was clicked (in order to add labeling and other features down the line). I have been trying to add this via a MeshCollider on the GameObject and adding raycasting from the mouse but have not been able to get it working properly.

I have the following attached to an empty game object in my scene

public class DigitalTwinSceneManager : MonoBehaviour
{
    public GameObject _scan;

    void Start()
    {
        _scan = (GameObject)Instantiate(Resources.Load("ZEDMesh"));
        MeshCollider mc = _scan.AddComponent<MeshCollider>();
        MeshFilter mf = _scan.AddComponent<MeshFilter>();
        _scan.AddComponent<MeshRenderer>();
        mc.convex = true;
        mc.sharedMesh = mf.mesh;
    }
}

and this is attached to my Main Camera in scene

public class RayCast : MonoBehaviour
{
    Camera camera;

    void Start()
    {
        camera = Camera.main;
    }

    void Update()
    {
        //Draw Ray
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 100f;
        mousePos = camera.ScreenToWorldPoint(mousePos);
        Debug.DrawRay(transform.position, mousePos - transform.position, Color.blue);

        if (Input.GetMouseButtonDown(0)) 
        {
            Ray ray = camera.ScreenPointToRay(mousePos);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit)) 
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                cube.transform.position = hit.point;
                Debug.Log("Something has been hit!");
                Debug.Log(hit.transform.name);
            }
        }
    }
}

I added a Plane to my scene for testing and this works for detecting hits on the plane, however it does not detect the hits on my instantiated object. I have tried to make the components added to my object exactly the same as the plane but am still unsuccessful.

If anyone could provide any advice or direction for accomplishing this goal it would be greatly appreciated. If there’s any extra info that may be needed let me know!

Thanks!

My Working Solution:

public class RayCast : MonoBehaviour
{
    Camera camera;

    void Start()
    {
        camera = Camera.main;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0)) 
        {
            Hit();
        }
    }

    void Hit()
    {
        Ray ray = camera.ScreenPointToRay(Input.mousePosition);
        Debug.DrawRay(ray.origin, ray.direction * 50f, Color.cyan, 2000f);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 50f))
        {
            Debug.Log("Object name is: " + hit.collider.gameObject.name);


            MeshCollider meshCollider = hit.collider as MeshCollider;
            if (meshCollider == null || meshCollider.sharedMesh == null)
                return;

            Mesh mesh = meshCollider.sharedMesh;
            Vector3[] vertices = mesh.vertices;
            int[] triangles = mesh.triangles;
            Vector3 p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
            Vector3 p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
            Vector3 p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
            Transform hitTransform = hit.collider.transform;
            p0 = hitTransform.TransformPoint(p0);
            p1 = hitTransform.TransformPoint(p1);
            p2 = hitTransform.TransformPoint(p2);
            Debug.DrawLine(p0, p1, Color.red, 1000f);
            Debug.DrawLine(p1, p2, Color.red, 1000f);
            Debug.DrawLine(p2, p0, Color.red, 1000f);
        }
        else
        {
            Debug.Log("Hit nothing");
        }
    }
}
public class DigitalTwinSceneManager : MonoBehaviour
{
    void Start()
    {
        GameObject _scan = (GameObject)Instantiate(Resources.Load("ZEDMesh"));
    }
}