# This.scene is undefined

**URL:** <https://phaser.discourse.group/t/this-scene-is-undefined/2136>\
**Category:** Phaser 3\
**Created:** [April 17, 2019, 4:07am UTC](https://phaser.discourse.group/t/this-scene-is-undefined/2136 "2019-04-17T04:07:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![linnie](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnie](https://phaser.discourse.group/u/linnie)\
**Post date:** [April 17, 2019, 4:07am UTC](https://phaser.discourse.group/t/this-scene-is-undefined/2136/1 "2019-04-17T04:07:52Z")

</div>

Hey.

I’m currently working on a small game, and recently I’ve been trying to incorporate scenes into my game so I can have a new scene after the player completes the level. However, every time I try to invoke `this.scene.start('EndScene');`, it returns `TypeError: this.scene is undefined`.

Here is a snippet of my code:

```javascript
function playerCollideEnd(){
      console.log('ya made it');
      this.scene.start('EndScene');      
    }

```

In fact, any time I try to invoke `this.scene.get('GameScene');`, or any similar function in my GameScene, it will return the undefined error. Please let me know if you want to see anymore code.

Thanks.

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yannick/32/732_2.png) [@yannick](https://phaser.discourse.group/u/yannick)\
**Post date:** [April 17, 2019, 11:07am UTC](https://phaser.discourse.group/t/this-scene-is-undefined/2136/2 "2019-04-17T11:07:00Z")

</div>

What is the context inside of `playerCollideEnd()`?

```javascript
function playerCollideEnd() { 
  console.log(this); 
}

```

---

<div class="post-metadata">

**Author:** ![linnie](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnie](https://phaser.discourse.group/u/linnie)\
**Post date:** [April 17, 2019, 4:12pm UTC](https://phaser.discourse.group/t/this-scene-is-undefined/2136/3 "2019-04-17T16:12:56Z")

</div>

Thanks for replying.

`console.log(this);` will log `function playerCollideEnd()` to the console. The function itself is called when the player collides with the end marker. This is all located within `class GameScene extends Phaser.Scene` under the `create()` function.

`console.log(this);` outside of any functions but still within `create()` logs `Object { sys: {…}, game: {…}, anims: {…}, cache: {…}, plugins: {…}, registry: {…}, scale: {…}, sound: {…}, textures: {…}, facebook: {…}, … }`.

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yannick/32/732_2.png) [@yannick](https://phaser.discourse.group/u/yannick)\
**Post date:** [April 17, 2019, 4:34pm UTC](https://phaser.discourse.group/t/this-scene-is-undefined/2136/4 "2019-04-17T16:34:03Z")

</div>

So, you somehow have to pass a reference to the scene inside `playerCollideEnd()` .  
Maybe with an arrow function?

```javascript
const playerCollideEnd = () => { 
  console.log(this); // this will be the scene
  console.log('ya made it'); 
  this.scene.start('EndScene'); 
}

```

Also, have you added the proper “key” into `super()`?

```javascript
class EndScene extends Phaser.Scene {
  constructor() {
    super({ key: 'EndScene' })
  }
}

```

---

<div class="post-metadata">

**Author:** ![linnie](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@linnie](https://phaser.discourse.group/u/linnie)\
**Post date:** [April 17, 2019, 4:48pm UTC](https://phaser.discourse.group/t/this-scene-is-undefined/2136/5 "2019-04-17T16:48:48Z")

</div>

Okay, that makes sense. I tried using the arrow function but then unfortunately the `playerCollideEnd()` didn’t function as a callback for a collider. However! I just made an arrow function that `playerCollideEnd()` calls upon and it works! Thanks so much.

Here’s what I fully did:

```javascript
function playerCollideEnd() { 
  console.log('ya made it'); 
  endFunc(); //not a good name i know
}
const endFunc = () => {
      this.scene.start('EndScene'); 
}

```

Thank you so much!

---

<div class="post-metadata">

**Author:** ![Skwal](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/skwal/32/1212_2.png) [@Skwal](https://phaser.discourse.group/u/Skwal)\
**Post date:** [April 17, 2019, 7:03pm UTC](https://phaser.discourse.group/t/this-scene-is-undefined/2136/6 "2019-04-17T19:03:41Z")

</div>

I highly suggest you read a bit more about JavaScript’s scope and closures and what ‘this’ represents in each context (maybe something like [https://javascriptplayground.com/javascript-variable-scope-this/](https://javascriptplayground.com/javascript-variable-scope-this/) or [https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback)). It will help you a lot in the future when you face this kind of issue.

---

<div class="post-metadata">

**Author:** ![Firemage0](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@Firemage0](https://phaser.discourse.group/u/Firemage0)\
**Post date:** [January 29, 2021, 9:18am UTC](https://phaser.discourse.group/t/this-scene-is-undefined/2136/7 "2021-01-29T09:18:02Z")

</div>

I have the same problem, but I can’t figure out how to solve it.

class title extends Phaser.Scene {  
constructor() {  
super({key: ‘title’});  
}  
preload() {  
var socket = io();  
}  
create() {  
socket.on(‘acceptedplayer’, function() {  
this.scene.start(“main”);  
});  
}  
}
