[SOLVED] Reference grandchild entity

I am trying to make references to entities with a template in my scene.

For direct children of the entity to which this script is attached, this works:

for (var i = 0; i < children.length; i++) {
    if (children[i].name === 'objectOne') {
        objectOneEntity = children[i];

Now I have one entity which is not a direct child but a grandchild. How would I reference to that?
Right now I’m trying something like this:

for (var i = 0; i < children.length; i++) {
    if (children[i].children[i].name === 'objectTwo') {
        objectTwoEntity = children[i];

Hi @Timo!

I’m not exactly sure what you try to do, but to get the first child of an entity you can use entity.children[0]

Thanks for the reply! Below you can see my scene hierarchy. Right now with the code I have I can access the first children of ‘PositionOneVehicles’. Now, I am trying to access the ‘Cross’ entity. Since it’s not a direct child entity.children[0], does not work.


If children[i] is the PositionOneVehicles entity, it will be children[i].children[0].children[0].

1 Like

You could also do something like:

const grandChild = PositionOneVehicles.findByName('Cross');
// where PositionOneVehicles is a parent entity

Just keep in mind that searching is expensive, so don’t do it in update method or too often.

1 Like

Aah alright. So I forgot to mention that the script is attached to ‘PositionOneVehicles’ so it will be one less ‘children[0]’ right?

I think it will work like this! It’s in the initialize part of the code so that should be fine.

const grandChild = this.entity.findByName('Cross');

I got this method to work aswell. Thank you!

for (var i = 0; i < children.length; i++) {
    } else if (children[i].children[0].name === 'Cross') {
        crossEntity = children[i];

Goed gedaan, bedankt voor het delen! :ok_hand:

1 Like