Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question CompositeCollider2D's GetPathPointCount after calling Tilemap.SetTile is zero

Discussion in '2D' started by Mallaboro, Dec 21, 2022.

  1. Mallaboro

    Mallaboro

    Joined:
    Sep 22, 2017
    Posts:
    18
    I need to get my
    Composite Collder 2D
    PathPointCount
    & paths on
    Start
    after generating a
    Tilemap
    on launch, but it's throwing an error because
    PathCount
    is zero.

    After the Start method ends and the first Update call occurs, I can access this data, but I'd rather have this all done on Start and not put some flunky code in an Update method.

    I've tried refreshing the
    Tilemap
    and calling
    CompositeCollider2D.GenerateGeometry
    but that doesn't help. I've tried both manual and synchronous generation.

    When in manual, collision doesn't work (almost as if the generate geometry call didn't go through). When in synchronous the collision works, but again, the issue I'm experiencing is not being able to get path point count, and subsequently its paths.

    Here's some example code to demonstrate the steps I'm taking

    Code (CSharp):
    1.         _tileMapWalls.SetTile( position + data.position, TileRepository.GetTile[data.name] );
    2.  
    3.         _compositeCollider = GetComponent<CompositeCollider2D>();
    4.         _compositeCollider.GenerateGeometry();
    5.  
    6.         Vector2[] pathVerts = new Vector2[_compositeCollider.GetPathPointCount( 0 )];
    7.         _compositeCollider.GetPath( 0, pathVerts );
    8.         List<Vector2> _compositeVerts = new List<Vector2>( pathVerts );
    ArgumentOutOfRangeException: Path index 0 must be in the range of 0 to -1.

     
    Last edited: Dec 21, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Generate geometry is for when the composite is in manual mode, it doesn't go looking for tiles, it knows nothing about them at all; the CompositeCollider2D was created way before the Tilemap stuff was even a thing.

    AFAIK SetTile is being added to a queue (see Max Tile Change Count) so they are deferred most likely so this is a tile thing, not a CompositeCollider2D thing.

    Look at the TilemapCollider2D docs here.

    https://docs.unity3d.com/ScriptReference/Tilemaps.TilemapCollider2D-hasTilemapChanges.html
    https://docs.unity3d.com/ScriptReference/Tilemaps.TilemapCollider2D.ProcessTilemapChanges.html
    https://docs.unity3d.com/ScriptReference/Tilemaps.TilemapCollider2D-maximumTileChangeCount.html
     
    Mallaboro likes this.
  3. Mallaboro

    Mallaboro

    Joined:
    Sep 22, 2017
    Posts:
    18
    Cheers, Melv, I just needed to call ProcessTilemapChanges when calling Rebuild.
     
    MelvMay likes this.