How to add a value to a variable when it’s in a bracket

var Movement = pc.createScript('movement');

Movement.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onCollision, this);
};
var carrothealth = 10;
Movement.prototype.onCollision = function(event) {
    var carrothealth - 1;
    if (carrothealth = 0) {
   this.entity.destroy();
  }
};

I tried just subtracting it but it gave me an error and the bracket is the one that starts with Movement. prototype.onCollision

Your question is not related to PlayCanvas but JavaScript. Please do not get it personally, but you show a lack of understanding of the language. I suggest you to spend time working on this first.

Check how classes work in general, and how Javascript does it in particular. Also variables scope (JavaScript is tricky in this aspect), and comparison vs assignments.

Going to your code:

var Movement = pc.createScript(‘movement’);

Movement.prototype.initialize = function() {
	this.entity.collision.on(‘triggerenter’, this.onCollision, this);
};
var carrothealth = 10;
Movement.prototype.onCollision = function(event) {
	var carrothealth - 1;
	if (carrothealth = 0) {
		this.entity.destroy();
	}
};

This is obviously not finished, but you would need something along these lines:

var Movement = pc.createScript(‘movement’);

Movement.prototype.initialize = function() {
    this.entity.collision.on(‘triggerenter’, this.onCollision, this);
    this.carrothealth = 10;
};

Movement.prototype.onCollision = function(event) {
    if (this.carrothealth == 0) {
        this.entity.destroy();
    }
};

Movement.prototype.getDamage = function(event) {
    this.carrothealth -= 1;
};
3 Likes

thank you