[SOLVED] Play Button doesnt work in another scene

Hello guys so yesterday i created Play Button which is shown when you finish the level. It works in first scene, but once im redirected to Level 2 and pass the finish line my button script is no longer working.

Here i created global event for enabling play button on finish:

I also unsubscribe to global event: image

Here i fire the event in finish: image

This part is working as the button is shown properly.

Lets see Play Button script.

Here I highlighted part of code i want to run when button is clicked.
It is working in first level I play, not in the second one. If I start to play from level 2 its working, but not working in level 3.

Do I need to unsubscribe to those mouse/touch events too?

Could you share a Project link? I think it is easier to help if people can see the whole thing.

1 Like

Hello, of course it is better to see, unfortunately the game is set private for reason and i cant provide inside peeks of the game. If there were only 3 levels i’d not care but there are over 30 levels which somebody can easily copy. But im willing to show you anything related to the button.

Code looks fine at a glance. I recommend using the browser debugger and try to work out where issues are when pressing the play button.

Things that I would check are:

  • Is the event actually being fired?
  • Are the button callbacks being called?
  • Is the correct play button being shown?
2 Likes

When you say “scene” do you mean the PlayCanvas scene, or your level, which all are under the same scene?
You also say “create global event”, then you unsubscribe “global event” on destroy. Do you subscribe back after you create a new one?

Play Canvas scene = in game Level -> Play Canvas scene 2 = in game Level 2

I think the event is subscribed everytime new scene is Loaded as those events are in initialize and also are in next level, the script with events is in every Scene

Well, the code snippets you showed seems to be fine, so can’t tell what can be a problem from the glance. This needs debugging.

Edit:
You might want to check that this context is correct:

1 Like

Good catch @LeXXik, that could be holding onto an old reference from a previous scene.

1 Like

Im not sure guys where you heading, play button is attached in inspector which im doing in every scene. context variable is holding ‘this’ . it is placed right after initialize: image

What I do, and what already is been advised, is to put console.log("number") on all places in the scripts that should be passed. This way you can see in the console of your browser after which number it goes wrong. For example is the step before, the onPress function, called?

1 Like

Sure I will debug now for a while, i will let you know where did I get with that.

EDIT: first this i got to is the onPress function, it seem that button is being pressed properly, although the onSkipLevel is not doing its job.

Sure I will debug now for a while, i will let you know where did I get with that.

EDIT: first this i got to is the onPress function, it seem that button is being pressed properly, although the onSkipLevel is not doing its job.

EDIT2:


Also we can see that onSkipLevel event is passing OK. That takes us in the script where skipLevel is defined.

EDIT3: I think i fixed it, this was part of code before the fix:
image

and here is after:
image

I was calling bad event to unsubscribe
And there was event being unsubscribed in delay, dunno why.
Im not sure about one line this changed for context(var context = this), and one line with this without context

Right, so context issues are hard to see from code snippets and require access to debug tools. For example, as you mentioned, context is holding this, however this may point to a different instance of a ScreenTimer script than you’d think.

1 Like

Web debugger is the way for fixing indeed. But im not always sure where to start with debugging.

EDIT: taking a rest is also a good way haha!

1 Like

After some time there will be little to no issues you haven’t fought with before, so you’ll recognize them quickly and know where to dig.

1 Like

Yes that is quite right ^^ Maybe one day i will get to that point.

Just curious why you do var context = this;?

I read somewhere that you have to wrap this into variable when you wanna use this inside the function. And that is actualy true because back at the day i tried to use this inside the function and it threw me an error, when I wrapped this into variable and used context instead of this then i worked.

Ah okay, that’s true indeed. You can also do this:

setTimeout(function(){
    this.entity.enabled = false;
}.bind(this), 1000);

As far as I can see you already do something like that in your other functions.

Anyways, I think you are doing really well and your game is going to look great!

1 Like

Thank you for advice.

Thank you so much, means a lot! :slight_smile:

1 Like