Regenerate CompositeCollider2D at runtime

I’m trying to regenerate CompositeCollider2D at runtime. Code as simple as

private void TileColliderTest()
    Vector3Int tilePosition = new Vector3Int(-7, -3, 0);

    print(SolidsTilemap.GetColliderType(tilePosition)); // Sprite
    SolidsTilemap.SetColliderType(tilePosition, Tile.ColliderType.None);
    print(SolidsTilemap.GetColliderType(tilePosition)); // None

    CompositeCollider2D cc2d = SolidsTilemap.GetComponent<CompositeCollider2D>();
    cc2d.generationType = CompositeCollider2D.GenerationType.Manual;

and nothing happens. But if i press “Regenerate Collider” in inspector it regenerates collider immediately. Is it a bug, or am i forgetting something?


Try changing setting CompositeCollider2D.GenerationType to Synchronous from Manual.


Try waiting 1 frame after updating tilemap content.

using UnityEngine; 
using UnityEngine.Tilemaps;
using System.Collections;

public class Playground : MonoBehaviour {
    [SerializeField] private TileBase tile;
    [SerializeField] private Tilemap tilemap;
    private IEnumerator Start()
        for (int x = 0; x < 3; x++)
            for (int y = 0; y < 3; y++)
                tilemap.SetTile(new Vector3Int(x, y, 0), tile);

        yield return new WaitForEndOfFrame(); // need this!

        yield return new WaitForEndOfFrame();

Above code can update collider with dynamic tile generation.
But, if we remove “// need this!” line, collder can not update.

I expect that this is bug of any component.

If you can use this workaround, please try it.