# How to get specific path in a composite collider2d

Is there any way for me to reference a specific path in a composite collider 2d? I want to be able to access a path’s collider. Thanks!

I’m not sure I understand your question 100%, but if I do, then here’s what I’d recommend:
You can get paths and points in a composite collider in a seemingly unnecessarily convoluted way. Using the compositeCollider.pathCount property, you can get the number of paths a composite collider has, then using compositeCollider.GetPath(int, Vector2) you can get the paths. So you could have a loop like this:

for (int i = 0; i < compositeCollider.pathCount; i++)
{
Vector2[] points = new Vector2[compositeCollider.GetPathPointCount(i)];
compositeCollider.GetPath(i, points);
}

Now what’s confusing about this is that GetPath returns an int, and that you must pass a Vector2 array into GetPath. This is because a path is just a series of points, and the array you pass in will contain those points after you call the method. The compositeCollider.GetPathPointCount(i) method will return the number of points that a given path has, where the i you pass in will get you the path at that index. So this allows you to create a new Vector2 array that is the size needed to hold all of your points at a given path. You then run the compositeCollider.GetPath(i, points) method to fill the array points with all of the points at index i.

Now if you’re trying to access a certain path’s collider, this is a bit more tricky, because points are just a series of Vector2s, and since they won’t hold a reference to a collider, things will get a bit wild. But if you have a path (Vector2) already, and you want to find a corresponding collider, what you could do is loop through all available CompositeCollider2Ds, the simplest way of doing so would be with FindObjectsOfType<CompositeCollider2D>() and then looping through them. You could then compare the contents of the paths in each composite collider to your path. There’s a few issues with this, because the paths are going to be Vector2 arrays, a reference type, you won’t be able to compare them directly, but instead compare their contents. Another issue is that if two colliders were to share the same path, you could get the collider you don’t want.

foreach (CompositeCollider2D compositeCollider in FindObjectsOfType<CompositeCollider2D>())
{
for (int i = 0; i < compositeCollider.pathCount; i++)
{
Vector2[] points = new Vector2[compositeCollider.GetPathPointCount(i)];
compositeCollider.GetPath(i, points);
bool colliderHasMatchingPaths = true;
foreach (Vector2 vector2 in points)
{
if (!myPath.Contains(vector2)) // Checks each point to see if the path you specify
{                              // matches the current collider's paths.
colliderHasMatchingPaths = false;
}
}

// If all points matched, then this will be true...
if (colliderHasMatchingPaths)
{
return compositeCollider; // So this is the collider you want
}
}
}