MeshCollider[] C#

Hi,

My objects position has a Mesh Collider attached to it. What the below code is doing, is only taking into account "1" of the game objects. I need it to consider the many game objects (that have a Mesh Collider attached it), within the Players distance from them.

`if (Vector3.Distance(lastPosition, objectPosition) < 125f)
{// if we are within the distance of the object

        lastPosition = transform.position;
        Debug.Log("Hello");

        if (Vector3.Distance(oldPosition, transform.position) < 0.01f)
        {
            // if we move
            pause_varaible = true;
        } 
        if (! pause_varaible)
        {
            // increment varaible
            pause_varaible = false;
        }
        // else the above is greater.
        oldPosition = transform.position;
    }
    else if (Vector3.Distance(lastPosition, objectPosition) > 250f)
    {
        lastPosition = transform.position;
        if (Vector3.Distance(oldPosition, transform.position) < 0.01f)
        {
            pause_varaible = true;
            //Debug.Log("Stopped Moving");
        }
        else if (Vector3.Distance(oldPosition, transform.position) > 0.01f)
        {
            pause_varaible = false;
        }
        if (! pause_varaible)
        {
            // decrement varaible
        }
        oldPosition = transform.position;
    }

`

Its not an "ideal" peice of code for what I want to happen, so I was wondering if I could simply it with:

MeshCollider [] mCol = (MeshCollider []) Physics.OverlapShere(transform.position, 125f);

To get all Game Objects that have a MeshCollider within the Players Distance from the Game Objects?

You can't cast Collider[] to MeshCollider[] like that, but it's close (Note: need to add using System.Collections.Generic; at the top of your script)

Using a temporary List

Collider [] colliders = Physics.OverlapShere(transform.position, 125f);
List<MeshCollider> meshColliders = new List<MeshCollider>();

foreach(Collider col in colliders)
{
    if(col is MeshCollider) meshColliders.Add((MeshCollider)col);
}

//Do stuff with meshColliders list here

Using the collider when looping through the first array:

Collider [] colliders = Physics.OverlapShere(transform.position, 125f);

foreach(Collider col in colliders)
{
    if(col is MeshCollider)
    {
        MeshCollider meshCollider = (MeshCollider)col;
        //do stuff with meshCollider
    }
}