Problems with a property

I have a problem with this.raycastPlayerBase.getPosition () and I get the following error “Cannot read property ‘getPosition’ of null”, perform a console log of raycastPlayerBase and it returns me null, but I had already defined it at the beginning.
I am quite a newbie and the error is probably simple, I apologize for that.
I leave the complete code here:

var PlayerMovement = pc.createScript('playerMovement');

PlayerMovement.attributes.add('speed', { type: 'number', default: 0.09 });

PlayerMovement.attributes.add('movementMultiplier', {
    type: 'number',
    default: 2.0
});

PlayerMovement.attributes.add('jumpPower', {
    type: 'number',
    default: 200.0
});

PlayerMovement.attributes.add('raycastPlayerBase', {
    type: 'entity'
});

PlayerMovement.prototype.initialize = function () {
    var app = this.app;
    var camera = app.root.findByName('Camera');
    this.cameraScript = camera.script.cameraMovement;
    this.force = new pc.Vec3();
    this.jumping = {
        state: false
    };
    this.running = {
        state: false
    };
};

PlayerMovement.prototype.update = function (dt) {
    var app = this.app;
    
    var force = this.force;

    var forward = this.entity.forward;
    var right = this.entity.right;

    var x = 0;
    var z = 0; 

    if (app.keyboard.isPressed(pc.KEY_A)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S)) {
        x -= forward.x;
        z -= forward.z;
    }
    
    if (app.keyboard.isPressed(pc.KEY_SHIFT)) {
        this.running.state = true;
    } else {
        this.running.state = false;
    }
    
    
    if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
        if (this.jumping.state === false) {
            this.entity.rigidbody.applyImpulse(0, this.jumpPower, 0);
            this.jumping.state = true;
        }
    } else if (this.jumping.state === true) {
        
        if (this._checkBelow() !== null) {
            this.jumping.state = false;
        }
    }
    
    if (x !== 0 || z !== 0) {
        var pos = new pc.Vec3(x * dt, 0, z * dt);
        pos.normalize().scale(this.speed);
        pos.add(this.entity.getPosition());
        
        var targetY = this.cameraScript.eulers.x + 180;
        var rot = new pc.Vec3(0, targetY, 0);
        
        this.entity.rigidbody.teleport(pos, rot);
        
        if (this.running.state === true) {
            force.set(x, 0, z).normalize().scale(this.speed * this.movementMultiplier);
        } else {
            force.set(x, 0, z).normalize().scale(this.speed);
        }
        this.entity.translate(force);
        this.entity.rigidbody.applyForce(force);
        this.entity.rigidbody.syncEntityToBody();
        
    }
};

PlayerMovement.prototype._checkBelow = function() {
    return this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), this.raycastPlayerBase.getPosition());
};

From already thank you very much

Hi @Immolated,

That’s looks correct, but have you parsed your script in editor for the attribute to show and then reference an entity to it?

If not, then it’s justified to return null. Check this manual page on how to work with editor attriburtes:

https://developer.playcanvas.com/en/user-manual/scripting/script-attributes/

1 Like

Thank you, it is true that I completely forgot that detail. However, after doing so… the problem persists

Hmm, not sure why that happens if an entity is referenced.

Can you try to debug in your initialized method if the this.raycastPlayerBase is not null? The simplest way is to add a console.log() and check the browser console:

// on top of your initialized method
PlayerMovement.prototype.initialize = function () {
   console.log('Check property value: ', this.raycastPlayerBase);

Effectively, it returns me null

Are you using the script on a single entity only? Double check all entities using that script, reference an entity in that attribute. I don’t see any other reason for that property to be null.