How to get properties inside entity?

I’m trying to find out how I can achieve something similar to this:

var Light =  entity.camera.light;

Any help would be appreciated!

Hello @WHISPEROFWIND :wave:,

I am not sure you are asking about components or the way it is plot.

:arrow_right: If you are looking for components please take a look here: Components | Learn PlayCanvas

:arrow_right: If you are looking for in-depth camera properties you can look here: Camera | Learn PlayCanvas

:arrow_right: If you want to create object you can do something like this.

var myObj = {name:"abc"};
myObj.number = 111;
console.log(myObj.number, myObj.name);

Output: 111 abc

:arrow_right: If you want to assign variable you can assign it directly.
(but make sure object name is not used anywhere or you will break something existing)

this.entity.mySpecialData = {name:"xyz"};
this.entity.mySpecialData.number = 222;
console.log(this.entity.mySpecialData.number, this.entity.mySpecialData.name);

Output: 222 xyz

I hope it helps :bulb:

1 Like

I’m trying to ask if I can get an object in the root, if it’s the child of another entity, so kinda like this:

Root → Player → Camera → Light

I’m trying to get the script to save the light as a variable by going inside the player and camera children.

You can find the child by name or tag.
https://developer.playcanvas.com/api/pc.Entity.html#findByName
e.g.

var light = this.app.root.findByName('Light');
// i am assuming light have **myscript.js** script contains **myFunction** function
light.script.myscript.myFunction(); // so this way i can call function
light.script.myscript.myVarData = "value"; // this way i can assign value

There is also findByPath which sounds closer to what you want? https://developer.playcanvas.com/api/pc.Entity.html#findByPath

2 Likes