Adding one to a variable

This might sound ridiculous but how do i add one to a variable with a keybind.

look at my code

myScript.attributes.add("coinAmount", {type:"entity"});



//this code is in the update function
var x = 0

if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)){

x + 1;
}
this.coinAmount.element.text = x;

It shows 0 on the text but when i press my mouse button it does not increase

and the code that isn’t the attribute is in the update function

btw, what variable do you guys use except x

  • x
  • y
  • n

0 voters

@jus.co_ding_me_23 Hi,I think maybe you should use x += 1 instead of x + 1

You probably want to do something like this instead:

var MyScript = pc.createScript('myScript');

MyScript.attributes.add('coinAmount', { type: 'entity' });

// initialize code called once per entity
MyScript.prototype.initialize = function() {
    this.x = 0;
};

// update code called every frame
MyScript.prototype.update = function(dt) {
    if (this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)) {
        this.x++;
        this.coinAmount.element.text = "" + this.x; // <= This will convert this.x to a string
    }
};

You can use this code:

myScript.attributes.add("coinAmount", {type:"entity"});



//this code is in the update function
var x = 0

if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)){

x++;
}
this.coinAmount.element.text = x;