[SOLVED] FPS game bullet counter

I’m trying to add a bullet counter system to my FPS game project. This is my script:

var BulletCounter = pc.createScript('bulletCounter');
BulletCounter.attributes.add('maxAmmo', { type: 'number', default: 10 });
BulletCounter.attributes.add('magazineSize', { type: 'number', default: 30 });
BulletCounter.attributes.add('bullet', { type: 'entity' });
BulletCounter.attributes.add('gunpower', { type: 'number', default: 1 });
BulletCounter.attributes.add('bulletCount', { type: 'entity' });
BulletCounter.attributes.add('currentAmmo', { type: 'number' });

// initialize code called once per entity
BulletCounter.prototype.initialize = function () {
    this.currentAmmo = this.maxAmmo;

    var app = this.app;

    app.mouse.on("mousemove", this._onMouseMove, this);


    app.mouse.on("mousedown", function () {
        app.mouse.enablePointerLock();
        if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
            this.shoot();
        }
    }, this);
};

    // update code called every frame
    BulletCounter.prototype.update = function (dt) {
        this.bulletCount.element.text = this.entity.this.currentAmmo + '/' + this.entity.this.magazineSize;
        console.log('this works');
    };

    BulletCounter.prototype.shoot = function (dt) {
        this.currentAmmo--;

        // Clone the bullet as specified by the attribute
        var bullet = this.bullet.clone();
        // Add it to the game 
        this.app.root.addChild(bullet);

        var player = this.entity;
        var target = this.app.root.findByName('target');
        // Its force is in the direction the player is facing 
        this.force = new pc.Vec3();
        this.force.copy(target.forward);
        this.force.scale(this.gunpower);

        var pos = target.getPosition();
        var direction = target.forward;
        // Add it a little further if the player is already going fast
        // pos.add(direction.scale(5 + gun.rigidbody.linearVelocity.length() * 0.01));

        bullet.setPosition(pos);
        // var bulletRotation =  player.getRotation();
        // bullet.setRotation( bulletRotation );
        // bullet.rotateLocal(90,0,0);

        bullet.enabled = true; //Must enable after setting position!

        bullet.rigidbody.applyImpulse(this.force);

    };
};

I added a text element entity in the hierarchy and referencing the text in my script. I believe this line: “This.bulletCount.element.text = this.entity.this.currentAmmo + ‘/’ + this.entity.this.magazineSize;” should show the current number of bullets and the magazine size on the screen and everytime i shoot, the current number of bullets should reduce by one. I added a console.log under that line but i get no message in the console when i launch the game. I think something is wrong with that line(or another part of the script)but can’t find out what. I would appreaciate any kind of assistance for my problem

Why are your update and shoot functions in your initialize function?

Sorry for my inattention. I corrected it

after i corrected it i get this error: "[bulletCounter.js?id=104123110&branchId=03b4effa-a4e2-469f-9688-44559f58af48:28]: Cannot read properties of undefined (reading ‘currentAmmo’)

TypeError: Cannot read properties of undefined (reading ‘currentAmmo’)
at BulletCounter.update (https://launch.playcanvas.com/api/assets/files/Scripts/bulletCounter.js?id=104123110&branchId=03b4effa-a4e2-469f-9688-44559f58af48:28:54)
at ScriptComponent._scriptMethod (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:31761:22)
at ScriptComponent._onUpdate (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:31806:15)
at ScriptComponentSystem._callComponentMethod (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:77179:52)
at ScriptComponentSystem._onUpdate (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:77196:11)
at ComponentSystemRegistry.fire (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:743:21)
at Application.update (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:34165:19)
at Application.tick (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:34775:18)
at Application.start (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:34139:11)
at https://launch.playcanvas.com/editor/scene/js/launch.js:1:10549"

Now you cant start working through the error messages now that the functions are being called

error message points out this line: “this.bulletCount.element.text = this.entity.this.currentAmmo + ‘/’ + this.entity.this.magazineSize;” It points out where the problem is, i don’t need to search the entire script. There are no “error messages” there’s only one now

By the way there is no need for sarcasm and it is unprofessional

Whoops, that was a typo. It was meant to be: “Now you can start working through the error messages now that the functions are being called”

No sarcasm intended

Okay, i will have to take your word for it. But seriously you could be a little more helpful. Anyway i’m grateful for the help you have provided me so far. By the way i fixed the issue. The correct line is: “this.bulletCount.element.text = this.currentAmmo + ‘/’ + this.magazineSize;”

I got it right the first time but i forgot to close the bracket of the initiate function and didn’t notice it. Then i added this.entity to that line bcs there was something similar in the unity tutorial and that was also wrong. Lucky for me my brother is a front end developer whose pretty good with JS, he spotted the mistake right away and i corrected it