getPosition() returning [NaN, NaN, NaN]

Hi, I am trying to store the position of an entity that is referenced in an script. The problem is that the following line:

console.log("Pos: " + this.refEntity.getPosition());

Shows “Pos: [NaN, NaN, NaN]” in the initialize and the first update. The crazy thing is that the second updated (and the following ones) shows a correct value.

Here is the complete script:

pc.script.attribute('refEntity', 'entity', null);

pc.script.create('CameraManager', function (app) {
    // Creates a new CameraManager instance
    var CameraManager = function (entity) {
        this.entity = entity;

    CameraManager.prototype = {
        // Called once after all resources are loaded and before the first update
        initialize: function () {

              console.log("Pos: " + this.refEntity.getPosition());


        // Called every frame, dt is time in seconds since last update
        update: function (dt) {

             console.log("Pos: " + this.refEntity.getPosition());


    return CameraManager;

this is probably wrong, but

im going to hazard a guess that the refentity hasn’t loaded when this script initialises -
to avoid throwing an error could you just add

if (this.refentity) { [get the position] };

Also, you havent actually captured the refentity as a variable from the script attribute … do you need (in initialise);
this.refentityname = app.assets.getAssetById(this.refentity); ?

sorry if this is obvious - :wink:

1 Like

@extropy is definitely on to something here. :smile:

You should avoid doing anything in a script’s initialize function that depends on some other entity in your scene hierarchy. This is because it may not yet have been initialized itself.

You might want to instead use the ‘postInitialize’ function, since when that is called, all of your entities are guaranteed to be initialized.