Refreshing the Polygon Collider (2D) upon sprite change?

Upon about an hour of research on the topic, I cannot find an answer. Currently, I am changing the sprite of a Sprite Renderer (Changing a weapon image in-hand). The Polygon Collider 2D does not update to the new sprite and shows the vertices of the initial sprite. Is there a way for me to refresh the the Polygon Collider to fit the newly attached sprite?

Warm Regards!

I didn’t find any way to automatically refresh the PolygonCollider2D, but you can do it manually:

polygonCollider = GetComponent<PolygonCollider2D>();
sprite = GetComponent<SpriteRenderer>().sprite;

for (int i = 0; i < polygonCollider.pathCount; i++) polygonCollider.SetPath(i, null);
polygonCollider.pathCount = sprite.GetPhysicsShapeCount();

List<Vector2> path = new List<Vector2>();
for (int i = 0; i < polygonCollider.pathCount; i++) {
  sprite.GetPhysicsShape(i, path);
  polygonCollider.SetPath(i, path.ToArray());

FIXED: Temporary Solution

After changing the sprite through the GetComponent method,

GetComponent<SpriteRenderer>().sprite = NewSprite;

I destroyed and then programmatically reinitialized the PolygonCollider.


Doing this completely resets the PolygonCollider’s shape (hence deleting and adding a new instance). I understand this may be classified “bad coding practice” towards computing memory and resources, but in my defense it is not being used for animations; It is solely being used when a user changes their in-hand item (which isn’t called very much).

Please let me know if anyone finds a better solution.

In case anyone is looking for a decent solution in 2021:

using System.Collections.Generic;
using UnityEngine;

public static class Collider2DExtensions {
    public static void TryUpdateShapeToAttachedSprite (this PolygonCollider2D collider) {

    public static void UpdateShapeToSprite (this PolygonCollider2D collider, Sprite sprite) { 
        // ensure both valid
        if (collider != null && sprite != null) {
            // update count
            collider.pathCount = sprite.GetPhysicsShapeCount();
            // new paths variable
            List<Vector2> path = new List<Vector2>();

            // loop path count
            for (int i = 0; i < collider.pathCount; i++) {
                // clear
                // get shape
                sprite.GetPhysicsShape(i, path);
                // set path
                collider.SetPath(i, path.ToArray());

Just create a new script called Collider2DExtensions and paste this inside. Now you can call collider.TryUpdateShapeToAttachedSprite(); on any of your polygon collider components to automatically try to update to the attached sprite renderer.

Use “Advanced Polygon Collider” it does this for you and is free asset.

So I found a solution that works for refreshing colliders that I’ve testing in 5.6.3f1
I know this thread is from 2014/2015 but it might help someone since this is the first result in google.

    IEnumerator RefreshCollider(Collider2D col)
        // Remember to call this with StartCoroutine
        // This will make a collider that will trigger 
        // it's OnEnterState again.

        col.enabled = false;

        // Wait a frame so the collider can update 
        // it's status to false 
        yield return null;

        // Enable
        col.enabled = true;

        yield return null;

        // Force an update to the collider logic by nudging the 
        // the transform but will ultimately not move the object
        col.transform.localPosition += new Vector3(0.01f, 0, 0);
        col.transform.localPosition += new Vector3(-0.01f, 0, 0);

As for the OP’s post this may or may not work. But as another user suggested. Just make the weapons prefabs and spawn those in to replace the other weapon.

Creating a new Polygon Collider 2D in your Game will lower overall Perfomance. Basically after adding component we usally edit the polygon collider . Since Polygon Collider 2D automatically tons of points which are actually not needed.

Well my solution is set Empty GameObject instead your weapon.(We will use this as Pivot Point)
Now create the Prefabs of you Weapon.
Once you done you just need to instantiate the required Weapon i.e. Prefab in that empty GameObject.

For anyone stumbling onto this.

I found a solution that worked for me. My unity version is 5.5.1

I found a link from an answer by killer_mech on a StackOverflow forum.

This tutorial in the following link uses a method of creating an array of polygon colliders and enabling and disabling them accordingly. So in my loop where I update the sprite, I also updated the colliders from the array.