# Container doesn't render children when declared by 'new' keyword

**URL:** <https://phaser.discourse.group/t/container-doesnt-render-children-when-declared-by-new-keyword/5383>\
**Category:** Phaser 3\
**Created:** [March 3, 2020, 3:17am UTC](https://phaser.discourse.group/t/container-doesnt-render-children-when-declared-by-new-keyword/5383 "2020-03-03T03:17:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samba/32/2903_2.png) [@samba](https://phaser.discourse.group/u/samba)\
**Post date:** [March 3, 2020, 3:17am UTC](https://phaser.discourse.group/t/container-doesnt-render-children-when-declared-by-new-keyword/5383/1 "2020-03-03T03:17:06Z")

</div>

I am trying to declare `Container` with `new` keyword for code modularization (referencing [this article](https://rexrainbow.github.io/phaser3-rex-notes/docs/site/container/)). However, when I declare one with `new Container()`, it doesn’t render its child image/sprite. What am I doing wrong here? Please see my code below:

```
var config = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  backgroundColor: '#010101',
  parent: 'phaser-example',
  physics: {
    default: 'arcade',
    arcade: {
      gravity: { y: 200 },
      debug: true
    }
  },
  scene: {
    preload: preload,
    create: create
  }
};

var container1, container2;
var game = new Phaser.Game(config);

function preload () {
   this.load.image('mushroom', 'assets/sprites/mushroom2.png');
   this.load.image('lemming', 'assets/sprites/lemming.png');
}

function create () {
  var image1 = this.add.image(-40, 0, 'mushroom');
  var image2 = this.add.image(40, 0, 'lemming');

  container1 = this.add.container(100, 200);
  container1.add(image1);
  container1.setSize(128, 64);
  this.physics.world.enable(container1);
  container1.body.setVelocity(0, 200).setBounce(1, 1).setCollideWorldBounds(true);

  container2 = new Phaser.GameObjects.Container(this, 300, 200)
  container2.add(image2);
  container2.setSize(128, 64);
  this.physics.world.enable(container2);
  container2.body.setVelocity(0, 200).setBounce(1, 1).setCollideWorldBounds(true);

  // container1 renders image1, but container2 doesn't render image2.

  console.log(this.add.image, Phaser.GameObjects.Container);
  console.log(container1, container2);
  console.log(container1.list[0], container2.list[0]);
}

```

If you want to run this code, please go to [Phaser3 SandBox](http://labs.phaser.io/edit.html?src=src/game%20objects%5Ccontainer%5Carcade%20physics%20body%20test.js#), and copy & paste above code (sorry, I don’t know how to save my code there). Any help would be highly appreciated!

---

<div class="post-metadata">

**Author:** ![rexrainbow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/rexrainbow/32/51_2.png) [@rexrainbow](https://phaser.discourse.group/u/rexrainbow)\
**Post date:** [March 3, 2020, 4:58am UTC](https://phaser.discourse.group/t/container-doesnt-render-children-when-declared-by-new-keyword/5383/2 "2020-03-03T04:58:18Z")

</div>

A _new_ game object won’t be added to display list, or updating list, call this method to add this container to display list.

```javascript
this.add.existing(container2);

```

---

<div class="post-metadata">

**Author:** ![samba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samba/32/2903_2.png) [@samba](https://phaser.discourse.group/u/samba)\
**Post date:** [March 3, 2020, 5:15am UTC](https://phaser.discourse.group/t/container-doesnt-render-children-when-declared-by-new-keyword/5383/3 "2020-03-03T05:15:05Z")

</div>

Wow, it works like a charm after replacing  
`container2 = new Phaser.GameObjects.Container(this, 300, 200)`  
with  
`container2 = this.add.existing(new Phaser.GameObjects.Container(this, 300, 200))`

Thank you so much!!

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [March 3, 2020, 6:05am UTC](https://phaser.discourse.group/t/container-doesnt-render-children-when-declared-by-new-keyword/5383/4 "2020-03-03T06:05:58Z")

</div>

You may as well use 🙂

```
container2 = this.add.container(300, 200);
```
