Problems changing scenes

Im trying to make an easily expandable script to change scenes & I asked chatgpt for help but event hat script still isn’t working. This is what I have so far

var LevelSwitcher = pc.createScript('levelSwitcher');

// Initialize the script
LevelSwitcher.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.readyToSwitch, this);
};

// Switch level to a new scene by name
LevelSwitcher.prototype.switchLevel = function (sceneName) {
    var app = this.app;

    // Find the scene by name
    var scene = app.scenes.find(sceneName);
    if (scene) {
        app.scenes.loadSceneHierarchy(scene.url, function (err, loadedScene) {
            if (err) {
                console.error("Failed to load scene:", err);
            } else {
                console.log(`Scene switched to: ${sceneName}`);
            }
        });
    } else {
        console.error(`Scene not found: ${sceneName}`);
    }
};

// Collision callback to handle level switching
LevelSwitcher.prototype.readyToSwitch = function (otherEntity) {
    if (otherEntity.name === "Player") {
        console.log("Collision detected with Player");

        // Check the current scene name
        var currentSceneName = this.app.root.name;
        console.log(`Current Scene Name: ${currentSceneName}`);

        // Determine the next scene to load
        if (currentSceneName === "lvl 1") {
            this.switchLevel("lvl 2");
        } else {
            console.error("Scene transition logic not defined for this scene.");
        }
    } else {
        console.log(`Collision detected with: ${otherEntity.name}`);
    }
};

Hi @Death68093 and welcome!

If I’m correct, otherEntity is not the other entity, but the entity with the script itself. So the name that is given to this variable is a little misleading. You probably need to use otherEntity.other instead.

if (otherEntity.other.name === "Player") {
    // collision with player
}
1 Like

Never use artificial intelligence for code. It only causes problems and is no substitute for actual skill.

1 Like

i highly agree but im new to PC so I am using it for help until I get the hang of it

Is this the line that i think sets it:
Is it the ‘this’ at the end of it that chooses the entity that this is in? if so how can I set it to the otherEnttiy?

LevelSwitcher.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.readyToSwitch, this);
};

No, that’s to be able to use this in the collision event.

Did you already try to use my suggestion of my previous post?

I did try it & it didnt work.
Im just going to remove the collision with the flag which raises a new question. How do I make an entity, a button? for example if you look up the game “the corridor” I have the button

Alright, no collision at all? You can check this with the console of your browser for example. Try to add.something like console.log(otherEntity.name).

There can be many reasons why something is not working. Maybe your setup is incorrect as well. You can share an editor link of your project here, so other users can take a look.

1 Like

thank you. I will work on it :slight_smile: