# Cannot setText after first run through of the game/switching scenes

**URL:** <https://phaser.discourse.group/t/cannot-settext-after-first-run-through-of-the-game-switching-scenes/11074>\
**Category:** Phaser 3\
**Created:** [January 18, 2022, 7:58pm UTC](https://phaser.discourse.group/t/cannot-settext-after-first-run-through-of-the-game-switching-scenes/11074 "2022-01-18T19:58:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cats555](https://avatars.discourse-cdn.com/v4/letter/c/b77776/32.png) [@cats555](https://phaser.discourse.group/u/cats555)\
**Post date:** [January 18, 2022, 7:58pm UTC](https://phaser.discourse.group/t/cannot-settext-after-first-run-through-of-the-game-switching-scenes/11074/1 "2022-01-18T19:58:36Z")

</div>

I am new to Phaser (just started learning it this weekend) and just started following tutorials/playing around with it and I hit a bug that is puzzling me. I’m hoping someone here can enlighten me on what is going wrong?

So the first run through of the game works totally fine but when the game ends and I try to play again, I hit an error when I try to execute setText() to update the score on the screen:

```javascript
Uncaught TypeError: s is null
    setSize http://localhost/js/phaser.min.js:1
    updateText http://localhost/js/phaser.min.js:1
    setText http://localhost/js/phaser.min.js:1

```

So when the game ends, I exit my game scene by simply calling the game over scene with this.scene.start(). Then, I re-enter my game scene by calling this.scene.start() from the game over scene, and again, this re-entry is when I am unable to update the score anymore.

I instantiate the scoreboard object (which ultimately will update the text) in my main game scene and use EventEmitters to detect when to update the score. All of that code leading up to calling setText() seems to work fine. But something goes wrong when setText() is called. I have confirmed that my setText() inputs are valid (not null).

I looked through the Phaser code on Github and I see that setText() calls updateText(), which then calls setSize() for the frame. The error I am seeing is occurring on line 376 in setSize(): [phaser/Frame.js at 5c8ecbcf999e6f328d21884e877c9e5935d2d350 · photonstorm/phaser · GitHub](https://github.com/photonstorm/phaser/blob/5c8ecbcf999e6f328d21884e877c9e5935d2d350/src/textures/Frame.js)

Does anyone know why `this.data` on line 375 would be null and how to fix this?

Also it seems like if I destroy the game and re instantiate `new Phaser.Game` after the game ends, this error does not occur. I am assuming this method is not good practice though and we shouldn’t have to destroy and re-instantiate Phaser.Game after each game run?

---

<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:** [January 18, 2022, 8:05pm UTC](https://phaser.discourse.group/t/cannot-settext-after-first-run-through-of-the-game-switching-scenes/11074/2 "2022-01-18T20:05:39Z")

</div>

👋

You probably need to remove the event listener when stopping the scene.

---

<div class="post-metadata">

**Author:** ![cats555](https://avatars.discourse-cdn.com/v4/letter/c/b77776/32.png) [@cats555](https://phaser.discourse.group/u/cats555)\
**Post date:** [January 18, 2022, 11:32pm UTC](https://phaser.discourse.group/t/cannot-settext-after-first-run-through-of-the-game-switching-scenes/11074/3 "2022-01-18T23:32:15Z")

</div>

Hi, thanks for the reply!

So I added a listener that will execute emitter.off() for those event listeners related to updating the score after the scene shuts down but I still see the same error afterwards.

The error seems to be because the frame data is somehow null - I am wondering what could cause this from scene switches?

---

<div class="post-metadata">

**Author:** ![cats555](https://avatars.discourse-cdn.com/v4/letter/c/b77776/32.png) [@cats555](https://phaser.discourse.group/u/cats555)\
**Post date:** [January 19, 2022, 4:03am UTC](https://phaser.discourse.group/t/cannot-settext-after-first-run-through-of-the-game-switching-scenes/11074/4 "2022-01-19T04:03:23Z")

</div>

Okay so I looked more into what is going on and I think I know what is happening. I am still unsure of how to properly resolve this though. Here are my observations:

So in GameScene create() - I create an instance of the ScoreBox class. This ScoreBox class creates this.scoreText via the method `this.scene.add.text()`.

`this.scoreText.setText()` is called whenever the EventEmitter detects that the score needs to be updated. For the first run of the game, all of this works totally fine.  
For debug purposes, I assigned `this.scoreText` a member variable with a random number, which came out to be 2 (`this.scoreText.debugNumber=2`).

After the game ends, I made a point to run `emitter.off(...)` for the events related to score updates, I run `this.scoreBox.scoreText.destroy()`, and set `this.scoreBox=null` prior to switching to the game over scene (although I get the same error even without doing this so this is probably not necessary).

In the second run of the game, the ScoreBox object is recreated when GameScene’s create() runs again, and a new `this.scoreText` variable is created. `this.scoreText` this time has a debugNumber value of 76.

When the game tries to run `this.scoreText.setText()` though, the debugger tells me that it is using the first `this.scoreText` that was already destroyed - the one with `this.scoreText.debugNumber=2`, instead of the newly created one. This is why this scoreText has a frame object with data=null.

How do I get the new `this.scoreText` object to be used instead? I already called emitter.off() on the event that calls for updating the score.

---

<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:** [January 19, 2022, 5:21pm UTC](https://phaser.discourse.group/t/cannot-settext-after-first-run-through-of-the-game-switching-scenes/11074/5 "2022-01-19T17:21:44Z")

</div>

Show the `on()` and `off()` code?

---

<div class="post-metadata">

**Author:** ![CAR182](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/car182/32/9297_2.png) [@CAR182](https://phaser.discourse.group/u/CAR182)\
**Post date:** [April 5, 2026, 1:22pm UTC](https://phaser.discourse.group/t/cannot-settext-after-first-run-through-of-the-game-switching-scenes/11074/6 "2026-04-05T13:22:00Z")

</div>

I got caught out by this one and spent ages digging out for a solution.

This is what worked for me:

```javascript
    this.scene.events.once(Phaser.Scenes.Events.SHUTDOWN, () => {
        EVENT_BUS.removeAllListeners();
    });

```

The singular removeListener() function didn’t seem to work. When running listenerCount it was still there. RemoveAll or Shutdown seemed to do the job.
