# Error with functions

**URL:** <https://phaser.discourse.group/t/error-with-functions/14091>\
**Category:** Phaser 3\
**Created:** [March 7, 2024, 3:55am UTC](https://phaser.discourse.group/t/error-with-functions/14091 "2024-03-07T03:55:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vegancatter](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@vegancatter](https://phaser.discourse.group/u/vegancatter)\
**Post date:** [March 7, 2024, 3:55am UTC](https://phaser.discourse.group/t/error-with-functions/14091/1 "2024-03-07T03:55:38Z")

</div>

my lines of code give me an error when I try giving the group `this.pipes` a child using the `.create()` function. The error msg disappears when I a different way of running the function shown at the bottom.

The method that gives error:

```javascript
function create () {
  this.pipes = this.physics.add.group();

  var spawnPipeTimer = this.time.addEvent({
    delay: 5000,
    callback: spawnPipe,
    callScope: this,
    loop: true
  
  });

  function spawnPipe() {
    var yOffset = ((Math.random() - 0.5) * 100) * 2

    var top_pipe = this.pipes.create(800, -50 + yOffset, 'top-pipe');
    var btm_pipe = this.pipes.create(800, 650 + yOffset, 'btm-pipe');

    top_pipe.setScale(4)
    top_pipe.setPosition(top_pipe.x , top_pipe.y + yOffset)
    this.pipes.add(top_pipe)

    btm_pipe.setScale(4)
    btm_pipe.setPosition(btm_pipe.x , btm_pipe.y + yOffset)
    this.pipes.add(btm_pipe)
  }
}

```

Error Msg:

```javascript
 Uncaught TypeError: Cannot read properties of undefined (reading 'create')

```

The Error Msg **disappears** when I use this method why is that?

```javascript
  this.pipes = this.physics.add.group();

  var spawnPipeTimer = this.time.addEvent({
    delay: 5000,
    callback: () => {
      var yOffset = ((Math.random() - 0.5) * 100) * 2
  
      var top_pipe = this.pipes.create(800, -50 + yOffset, 'top-pipe');
      var btm_pipe = this.pipes.create(800, 650 + yOffset, 'btm-pipe');
  
      top_pipe.setScale(4)
      top_pipe.setPosition(top_pipe.x , top_pipe.y + yOffset)
      this.pipes.add(top_pipe)
  
      btm_pipe.setScale(4)
      btm_pipe.setPosition(btm_pipe.x , btm_pipe.y + yOffset)
      this.pipes.add(btm_pipe)
    },
    callScope: this,
    loop: true
  
  });

```

---

<div class="post-metadata">

**Author:** ![pyf](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/pyf/32/7754_2.png) [@pyf](https://phaser.discourse.group/u/pyf)\
**Post date:** [March 7, 2024, 4:05am UTC](https://phaser.discourse.group/t/error-with-functions/14091/2 "2024-03-07T04:05:26Z")

</div>

this.pipes only exists within the create method, so using this.pipes within the spawnPipe method is naturally undefined. The arrow function’s ‘this’ points to that of create, so it can access this.pipes.

---

<div class="post-metadata">

**Author:** ![pyf](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/pyf/32/7754_2.png) [@pyf](https://phaser.discourse.group/u/pyf)\
**Post date:** [March 7, 2024, 4:29am UTC](https://phaser.discourse.group/t/error-with-functions/14091/4 "2024-03-07T04:29:26Z")

</div>

Changing ‘callScope’ to ‘callbackScope’ might make it work.

---

<div class="post-metadata">

**Author:** ![vegancatter](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@vegancatter](https://phaser.discourse.group/u/vegancatter)\
**Post date:** [March 7, 2024, 3:08pm UTC](https://phaser.discourse.group/t/error-with-functions/14091/5 "2024-03-07T15:08:49Z")

</div>

thxsm
