[SOLVED] Problems with script destruction

Ok so im trying to get the cameraMovement script to be destroyed after the player dies and this is what i have now:

var Player = pc.createScript('player');

// initialize code called once per entity
Player.prototype.initialize = function() {
        this.health = 100;
        this.bullets = 499;
        this.entity.collision.on('collisionstart', this.onCollisionStart, this);
        //this.entity.script.cameraMovement.enabled = true;
        this.entity.rigidbody.enabled = true;
};

// update code called every frame
Player.prototype.update = function(dt) {
};

Player.prototype.onCollisionStart = function (result) {
      if (result.other == this.player) {
          this.deadText.enabled = true;
          this.player.rigidbody.teleport(0, 0.947, 0);
          this.entity.script.destroy('cameraMovement');
          this.player.rigidbody.enabled = false;
          this.cameraEntity.teleport();
    }
};


// swap method called for script hot-reloading
// inherit your script state here
// Player.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

But the cameraMovement script still works

this.entity.script.destroy('cameraMovement');

I’ve never used this line, what I would do is destroy the player entity itself, set up speed=0 or another variable like canMove=false.

Maybe that line just doesn’t work because it’s not the way you should do it, therefore the listeners created when that script was instantiated are still there and not destroyed.

Where variable this.player is declared in your code? The condition is not fulfilled.

that line is used in the documentation of pc.SCRIPT_COMPONENT

this.player is meaning that the player itself is declared

I see, thanks for teaching me.

Are you sure that if block is run? Can you add a console.log there?
Are you sure before destroying the script instance, you had cameraMovement, and after destroying it, you don’t? console.log(this.entity.script.has('cameraMovement'))

Maybe I don’t quite understand, but I think the value of the variable this.player will be undefined. If you refer to the player’s entity, then in this case you should search for it in the root and save it to a variable. For example, using this method. In general, a link to the project would be useful.

https://playcanvas.com/project/612040/overview/animation-test sorry maybe i should have [posted the project link before
and @dadiaar i will try that

Just checked your project, I think @MasalovAndreey was right with the this.player thing.

So i should define the player as var player = this.app.root.findByName('Player');?
EDIT: Nvm mind i seen what i did wrong i didnt look through the root to find the player thx!

@MasalovAndreey & @dadiaar i did the console.log thing but still didnt work and the console isnt running as well im lost


As you can see the if block is being run but the script is not stopping i tried just disabling it but never worked
EDIT: im going to try making the destruction a function? i dont know i will keep trying

The script you want to delete is at the camera, not the player. Try adding to initialize this.camera = this.app.root.findByName('Camera'), and replacing this.entity.script.destroy('cameraMovement') on this.camera.script.destroy('cameraMovement').

1 Like

Worked thank you so much @MasalovAndreey i dont know how long it would have taken me to figure that out without you :slight_smile: sometimes i make those mistakes thx!

1 Like