How can I add 2 BoxCollider2D to GameObject by script

BoxCollider2D _box1 = new BoxCollider2D();
            _box1.size = new Vector2(0.2f,0.4f);
            _box1.offset = new Vector2(0,0.3f);
BoxCollider2D _box2 = new BoxCollider2D();
            _box2.size = new Vector2(0.4f,0.4f);
            _box2.offset = new Vector2(0,0.3f);
GameObject platform = new GameObject ();

How can i add box1, box2 to platform?

You don’t create components like this, you use GameObject.AddComponent ().

var box1 = platform.AddComponent<BoxCollider2D>();
var box2 = platform.AddComponent<BoxCollider2D>();
2 Likes

Thanks.