Access to components


Is there some method like GetComponent in Unity for Entities?

How can i get access to script componet of Entity, and also to script’s attributes in code?

You access a component on an entity like this: Entity . ComponentName . AttributeName.

For example, to get the field of view on a camera component, you’d do:

var fov =;

For a script component, you would do:

var myAttrib = this.entity.script.myScriptName.myAttrib;

‘myScriptName’ is what is the first parameter passed in to pc.script.create in your script file.

Definitely have a read through of the Scripting section in the User Manual.

1 Like


i’ve already tied this way, but it does not work - for example, i try to set attributes for sprite script, for my GUI elements:

var gui_test = new pc.fw.Entity(); // let this be entity for gui element, { // add sprite script
scripts: [{
url: ‘./scripts/sprites.js’

// and now try to set position (sprite - is a name of sprite script, x, y - names of attributes):
gui_test.sprite.x = 10; - // i’ve got error here, debuger shows that gui_test.sprite == undefined
gui_test.sprite.y = 20;

Maybe, sprite component is instantiated only after attach em to root element, but i need to set attributes before initialize method, that mean before attaching to root.

I think you need to do:

gui_test.script.sprite.x = 10;