title: How to match a box collider to a mesh with code in Unity?
body: <p>My problem:
  <a href="https://i.stack.imgur.com/KGsd7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KGsd7.png" alt="problem"></a></p>
  
  <p>This is a generated triangle. I want to add more vertices later (4,5,6...). For the moment I want to add an collider2d for this meshes. Later I have pentagons and hexagons, this is the reason why I think that I need a BoxCollider2D.</p>
  
  <p>After this:</p>
  
  <pre><code>        BoxCollider2D boxCollider = gameObject.AddComponent&lt;BoxCollider2D&gt;();
  </code></pre>
  
  <p>I get the above result. When I add this:</p>
  
  <pre><code>        MeshRenderer renderer = gameObject.GetComponent&lt;MeshRenderer&gt;();
      boxCollider.offset = renderer.bounds.center;
      boxCollider.size = renderer.bounds.size;
  </code></pre>
  
  <p>All my triangles are matched together:</p>
  
  <p><a href="https://i.stack.imgur.com/4lV87.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4lV87.png" alt="matched"></a></p>
  
  <p>And I get this error message in the added Box Collider2D:</p>
  
  <p><a href="https://i.stack.imgur.com/kDd7T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kDd7T.png" alt="error"></a></p>
  
  <p>I simply want to match the collider (green) to the mesh.</p>