Duplicated entities even though old scene is destroyed

s I am moving on to the next level my player gets duplicated as it goes to the next scene even though I have destroyed the previous hierachy:

this is the sample scene that i just placed at the end to give you an example. As you can see, for some reason the player get duplicated multiple times and this sometimes happen randomly throughout the levels which causes the errors to appear.

As a side question, where are you getting the code for loading a scene from? It’s pretty outdated :sweat_smile:

1 Like

haha I actually started playcanvas two days ago, and I realised that there are not many tutorials about it on coding. So I get whatever I can on youtube tutorials that are made years ago.

I recommend using the example project here to see how to change scenes with the current version of PlayCanvas: Changing scenes | Learn PlayCanvas

Without seeing the project, my best guest is that you are loading the new scene multiple times. Please share a link to the project and it be easier for people in the forums to look at

I did check this out before, but I thought I have already modified it to the recent one but ill check it out again. this is the link:

https://playcanvas.com/editor/scene/1318590

I’ve forked and updated your project to use the ‘correct’ scene loading code: https://playcanvas.com/editor/scene/1321076

https://playcanvas.com/editor/code/874764?tabs=66569786

It looks like in your project, you are were referring to the oldHierarchy in a different function and should be calling the callback instead. This resulted in an error (where you should have gotten an error message) and the old hierarchy not being deleted.

1 Like

sorry, but can I know which parts you have updated cause I cannot see the difference haha. I would like to learn for future reference, also which part did I do it wrong?

I changed this function:

NextScene.prototype.loadScene = function (sceneName, callback) {

    // Get the path to the scene
    var scene = this.app.scenes.find(sceneName);
    
    // Load the scenes entity hierarchy
    this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {
        if (!err) {
            callback();
        } else {
            console.error(err);
        }
    });
};

I ran it several times to test it but it did happen again. Is it because Im running it too many times?
pic:


two duplicated entities appeared this time

Looks like that is a different error that should be looked at. I would also change the NextScene code so that it can’t load the scene more than once per level

That error only happens when a duplicated entity made, at least that happens everytime when the duplication occurs. As for the NextScene code, I thought its already loading the scene once per level?

There’s nothing stopping multiple collisions callbacks to occur which will cause multiple calls to loadScene

Im sorry but how do I load a scene once per level?

I decided to try a ‘video reply’ in an open office hours format to explain the changes I’ve made. Let me know what you think :slight_smile:

Forked project: https://playcanvas.com/project/875817/overview/f-first-project-fork-scene-load

3 Likes

Very helpful. I learned programming by following a video tutorial series (making a chess and checkers game) that was made in the same way. Because you can follow every step in the process, you slowly start to understand the connections.

Oh wow, I really didnt expect this to happen at all. This was actually a test for a job interview, in which I tried to finish as best I could and submit it. I mostly use unity therefore this was very new to me to use and I was testing things out, and there was sadly not many tutorials on playcanvas. This video you just sent was really helpful and I have learned alot from it, thank you.

Is this for a playcanvas related job?

Yes, I needed to show that I could grasp the basics by implementing a simple game mechanic.