[SOLVED] Cannot read properties of undefined

https://playcanvas.com/editor/scene/1316007
Hello

Can someone help me? When i launch init scene then click the Scene3 it says:
Cannot read properties of undefined (reading ‘cameraMovement’)

But when i lauch Scene3 directly it doesn’t say it

project:
https://playcanvas.com/project/872349/overview/game1

When I try to do: Init Scene => Scene 3

1 Like

Hi @roman and welcome! Not sure what is the problem here, but if you change the name of the camera in scene 3 to for example ‘Camera3’ and you change this name also in your script, then the error is gone. So I guess the script finds a wrong entity with the name ‘Camera’ somewhere, that doesn’t have a script component.

2 Likes

Hi @roman !

If I had to guess, It’s because the scene manager (Thank you for forking!) loads the new scene before deleting the old one. I’ve run into issues in my own projects with this before, where I needed to delete the old scene earlier, but it can cause problems in other ways. Deleting the old scene earlier means that whatever is happening in the initialize functions of the new scene won’t be affected by entities with the same name sin the old scene, but it comes with the problem of your old scene being visually gone before the new one appears. I cover up all of my scene transitions with a loading screen, so it isn’t a problem for me, but since you haven’t, I would guess you would want to avoid the black scene.

Your best bet would be to use script attributes instead of this.app.root.findByName() to choose your camera in the playerMovement script. "This will ensure that the correct entity is always chosen regardless of when the scene is loaded.

I hope this is helpful!

2 Likes

Thank you, it works

It works after choosing the camera, thank you so much