I have tried changing scenes when the player lands on the goal (the green block), but it doesn’t seem to work. I used the same code in the Title Screen scene when the play button is pressed, and it works there. But somehow in the Level 1 and Level 2 scenes when the player lands on the goal, the scene isn’t switched to the next level. Please help me find a way to change the scene when the player lands on the goal.
Hi @InfinityLight and welcome!
Where do you define
Oh, that was the issue. It was there because of some old code that I had there before and I forgot to change it. I have changed it now, and I am getting errors sometimes when I land on the goal. It works sometimes, but not always.
Here is the error:
My guess is that this is a network issue. (Sinds you don’t get the error on
hierarchy). Maybe @yaustar knows more.
I have tried a lot of things and have finally gotten it to work. I used a
setTimeout function with a 1 second delay to make sure everything loaded properly and I don’t seem to get the error anymore. But I do get an error when I try to jump in the second level. It says
applyImpulse isn’t defined.
It basically means that no rigidbody component is found on the entity with the script. Could it be that you attached the script to an entity without a rigidbody component?
I checked all the entities, and none of them have the script except the player, who should have a rigidbody component. I also found out with a bit of testing that it is always the third scene that was changed to that the jumping doesn’t work.
For example: If I launch on the Title Screen, the problem occurs on Level 2, or if I launch on Level 2, the problem occurs on Level 1.
The scenes cycle from Title Screen to Level 1 to Level 2 and back to Title Screen.
Then there is probably something wrong with how you change the scenes. Can you please check the page below to compare your method?
All the code is exactly the same as the page. I even made sure by copy pasting the entire function. It still doesn’t seem to work though.
I’m no expert on this, but it seems to be related to the event listener. If I add the rule below just before loading a new scene, I no longer get the error.
this.app.keyboard.off(pc.EVENT_KEYDOWN, this.onKeyDown, this);
Apart from that, I don’t understand why you add a timeout after loading a new scene.
this.canTeleport is set to
true anyway in the
initialize function of the new scene.
Hello I forked your project and played around with it but did not run into any issues or error messages, for me everything is working fine after testing multiple times.
This is true the set timeout function does not do anything here as you are just setting the this.canTeleport variable to true after 1 second which is doing nothing as Albertos said you have the variable set to true on initialize already. You can just remove that and your script will work fine as it did for me.
If you want to add a delay to the teleport then put the loadScene function call inside the set timeout function instead which will delay your teleport to 1 second.
Also keep in mind when changing scenes if your player entity has something in scene1 that it does not have in scene2 then it will throw errors once you get to scene2. So make sure all player entities are completely identical in terms of components/scripts.
If it wasn’t there, I would just get the error where it said
Cannot read properties of null (reading 'settings')
My guess to why this was hapening was because sometimes when I jumped onto the goal, the player bounced a bit, making the
triggerEnter event fire multiple times while the first fire was still loading the scene. I made it wait one second to make sure it loaded fully, and it seemed to work.
And thank you @Albertos for the code. It works perfect now.
You prevent this already with the statement and setting
false. There is no reason to set it back to
true because this is already done when you load the new scene. I suggest to try it without the timeout and if succesfull remove the timeout.
It looks like it works when I remove the timeout. Thanks for the help!
I would also like to add that you need to clean up event listeners when entities are destroyed, especially if they are listening to events on the application object eg:
@yaustar How could I do this?
You can see an example here for the mouse event listeners
Thank you @yaustar