# GetShapes

> Gets all the PhysicsShape2D used by the Collider2D.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.0/script-reference/unityengine.md)
* **Assembly:** UnityEngine.Physics2DModule

## GetShapes(PhysicsShapeGroup2D)

Gets all the [PhysicsShape2D](/engine/6000.0/script-reference/unityengine/physicsshape2d.md) used by the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md).

```csharp
public int GetShapes(PhysicsShapeGroup2D physicsShapeGroup)
```

### Parameters

**** (\[PhysicsShapeGroup2D]\(/engine/6000.0/script-reference/unityengine/physicsshapegroup2d)): The [PhysicsShapeGroup2D](/engine/6000.0/script-reference/unityengine/physicsshapegroup2d.md) to store the retrieved [PhysicsShape2D](/engine/6000.0/script-reference/unityengine/physicsshape2d.md) in.

### Returns

| Type                                                       | Description                                                                                                                                                                                       |
| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | Returns the number of [PhysicsShape2D](/engine/6000.0/script-reference/unityengine/physicsshape2d.md) retrieved from the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md). |

### Remarks

All [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) represent a high-level definition of 2D geometry that is used to create efficient low-level primitive shapes used by the physics engine for collision detection. These low-level primitive shapes are what the 2D physics gizmos draw within the Unity Editor. This method provides access to a copy of these primitive shapes which can be useful in various use-cases such as debugging and gizmos or rendering [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) geometry at runtime.

Use this method to gain access to a copy of all the low-level primitive shapes that are produced by a [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md). If no such shapes exist on the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) as indicated by [Collider2D.shapeCount](/engine/6000.0/script-reference/unityengine/collider2d/shapecount.md) then the shape group will not be populated with shapes. If shapes do exist, the group is first emptied before being populated with shapes.

Each [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) produces either a single or multiple [PhysicsShape2D](/engine/6000.0/script-reference/unityengine/physicsshape2d.md) of a single [PhysicsShapeType2D](/engine/6000.0/script-reference/unityengine/physicsshapetype2d.md) as indicated below:

* A [CircleCollider2D](/engine/6000.0/script-reference/unityengine/circlecollider2d.md) produces a single [PhysicsShapeType2D.Circle](/engine/6000.0/script-reference/unityengine/physicsshapetype2d/circle.md) object
* A [CapsuleCollider2D](/engine/6000.0/script-reference/unityengine/capsulecollider2d.md) produces a single [PhysicsShapeType2D.Capsule](/engine/6000.0/script-reference/unityengine/physicsshapetype2d/capsule.md)  object
* An [EdgeCollider2D](/engine/6000.0/script-reference/unityengine/edgecollider2d.md) produces a single [PhysicsShapeType2D.Edges](/engine/6000.0/script-reference/unityengine/physicsshapetype2d/edges.md) object
* A [BoxCollider2D](/engine/6000.0/script-reference/unityengine/boxcollider2d.md) produces a single [PhysicsShapeType2D.Polygon](/engine/6000.0/script-reference/unityengine/physicsshapetype2d/polygon.md) object (a box is a convex polygon)
* A [PolygonCollider2D](/engine/6000.0/script-reference/unityengine/polygoncollider2d.md) produces either a single or multiple [PhysicsShapeType2D.Polygon](/engine/6000.0/script-reference/unityengine/physicsshapetype2d/polygon.md) object(s)
* A [TilemapCollider2D](/engine/6000.0/script-reference/unityengine/tilemaps/tilemapcollider2d.md) produces either a single or multiple [PhysicsShapeType2D.Polygon](/engine/6000.0/script-reference/unityengine/physicsshapetype2d/polygon.md) object(s) (each tile can produce multiple polygons so this can produce a lot of data)
* A [CompositeCollider2D](/engine/6000.0/script-reference/unityengine/compositecollider2d.md) produces either a single or multiple [PhysicsShapeType2D.Polygon](/engine/6000.0/script-reference/unityengine/physicsshapetype2d/polygon.md) ([CompositeCollider2D.GeometryType.Polygons](/engine/6000.0/script-reference/unityengine/compositecollider2d/geometrytype/polygons.md)) or [PhysicsShapeType2D.Edges](/engine/6000.0/script-reference/unityengine/physicsshapetype2d/edges.md) ([CompositeCollider2D.GeometryType.Outlines](/engine/6000.0/script-reference/unityengine/compositecollider2d/geometrytype/outlines.md)) object(s)

All [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) shape vertices are stored in the local space of the [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) they are attached to i.e. their vertices are relative to the pose of the [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) therefore when a [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) translates or rotates, the vertices of attached [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) do not change. This is why changes to position or rotation should only ever happen via the [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) as this is the most efficient method. When retrieving shapes, the shape vertices are in the local space of the [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md). The current position and rotation of the [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) is stored in [PhysicsShapeGroup2D.localToWorldMatrix](/engine/6000.0/script-reference/unityengine/physicsshapegroup2d/localtoworldmatrix.md) and can be used to transform the shape vertices to world space. If the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) is not attached to any [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) i.e. it is Static then the local space and world space are identical therefore the [PhysicsShapeGroup2D.localToWorldMatrix](/engine/6000.0/script-reference/unityengine/physicsshapegroup2d/localtoworldmatrix.md) is set to [Matrix4x4.identity](/engine/6000.0/script-reference/unityengine/matrix4x4/identity.md).

**Note:** It is recommened to reuse the [PhysicsShapeGroup2D](/engine/6000.0/script-reference/unityengine/physicsshapegroup2d.md) because then memory allocations only occur if the number of shapes or vertices retrieved are greater than the existing capacity of the [PhysicsShapeGroup2D](/engine/6000.0/script-reference/unityengine/physicsshapegroup2d.md). Reusing this object therefore minimizes allocations to a bare minimum and eventually to none so no work is produced for the garbage collector.

Additional Resources: [Rigidbody2D.GetShapes](/engine/6000.0/script-reference/unityengine/rigidbody2d/getshapes.md).

## GetShapes(PhysicsShapeGroup2D, int, int)

Gets the specified range of the [PhysicsShape2D](/engine/6000.0/script-reference/unityengine/physicsshape2d.md) used by the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md).

```csharp
public int GetShapes(PhysicsShapeGroup2D physicsShapeGroup, int shapeIndex, int shapeCount = 1)
```

### Parameters

**** (\[PhysicsShapeGroup2D]\(/engine/6000.0/script-reference/unityengine/physicsshapegroup2d)): The [PhysicsShapeGroup2D](/engine/6000.0/script-reference/unityengine/physicsshapegroup2d.md) to store the retrieved [PhysicsShape2D](/engine/6000.0/script-reference/unityengine/physicsshape2d.md) in.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The index of the first shape to retrieve. This should be in the range of 0 to [Collider2D.shapeCount](/engine/6000.0/script-reference/unityengine/collider2d/shapecount.md)-1.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The number of shapes to retrieve. The shapeIndex + shapeCount must be less than or equal to [Collider2D.shapeCount](/engine/6000.0/script-reference/unityengine/collider2d/shapecount.md).

### Returns

| Type                                                       | Description                                                                                                                                                                                                                                                   |
| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | Returns the number of [PhysicsShape2D](/engine/6000.0/script-reference/unityengine/physicsshape2d.md) retrieved from the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md). In all cases this should be the same number as shapeCount . |
