Collider2D.Bounds not working

I want to make a system that automatically detects how big my world bounds need to be, by measuring the most extreme point of the colliders in my world. The only colliders i currently have are one BoxCollider2D (trigger), one TilemapCollider2D and one CompositeCollider2D on the same object. However, the following code:

foreach (Collider2D c in World.GetComponentsInChildren<Collider2D>())
    Bounds b = c.bounds;
    Debug.Log("Bounds: " + b.max + ", " + b.min);

logs (0, 0, 0), (0, 0, 0) for the tilemap and composite colliders and the position of the box collider.

I have no idea why it’s happening, in the scene view when i check the BoxCollider2D it displays different bounds in the info box even.

Thanks for your time!

I realize now that the problem may have something to do with the fact that the colliders are part of a prefab, and not an active game object. The script is run in from the inspector.

Also, does anyone know how to add line breaks when asking a question?

3 Things I can think of could happen here. First to refer to the docs (Unity - Scripting API: Collider2D.bounds):

  • The collider is disabled
  • The GameObject is disabled
    Different reason:
  • The world might be scaled down so the log shows 0 because the sizes are of a smaller fraction. Try debugging with ToString(“G6”) put to max and min

Might be late to answer but i just got the same problem and found a solution when trying to instanciate a prefab and using the prefab to get the Colider bounds it didnt work but there is a get around i found where u can use a varaible to catch reference of the instantiated object and then the bounds will show correctly.