[SOLVED] Getting attribute values from another script

Hello! I am still learning and I dont understand why I cannot get an attribute values from another script. My setup is two scripts - One with the name PickerRaycast (which is attached to a camera) and one with the name Pos1Attributes which is attached to an box entity.

The basic idea is that I want to get the attribute values of the individual entities with a mouse click. When I try the code I get the error: Uncaught TypeError: Cannot read property ‘get’ of undefined

What am I doing wrong?
Thanks in advance!

var Pos1Attributes = pc.createScript('Pos1Attributes'); 
   
Pos1Attributes.attributes.add('x', {
    type: 'number'
});

Pos1Attributes.attributes.add('y', {
    type: 'number'
});

Pos1Attributes.attributes.add('z', {
    type: 'number'
});

Pos1Attributes.attributes.add('rotation', {
    type: 'number'
});


----------------------------------------------------------------------------
var PickerRaycast = pc.createScript('pickerRaycast');

// initialize code called once per entity
PickerRaycast.prototype.initialize = function() {
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onSelect, this);
};

PickerRaycast.prototype.onSelect = function (e) {
    var from = this.entity.camera.screenToWorld(e.x, e.y, this.entity.camera.nearClip);
    var to = this.entity.camera.screenToWorld(e.x, e.y, this.entity.camera.farClip);

    this.app.systems.rigidbody.raycastFirst(from, to, function (result) {
        var pickedEntity = result.entity;
        var data = {
            command: "Teleport",
            x: pickedEntity.attributes.get('x'),
            y: pickedEntity.attributes.get('y'),
            z: pickedEntity.attributes.get('z'),
            rotation: pickedEntity.attributes.get('rotation'),
        };
         });
    };

The raycast returns the entity, not the script component(s).

You would want something like this:

var pickedEntity = result.entity;
var pos1Script = pickedEntity.script.Pos1Attributes; // Where 'Pos1Attributes' is the name in script that is passed in pc.createScript
var data = {
            command: "Teleport",
            x: pos1Script.x,
            y: pos1Script.y,
            z: pos1Script.z,
            rotation: pos1Script.rotation,
        };
2 Likes

Thank you very much for the fast answer, it worked perfectly!