Timo
January 11, 2024, 1:09pm
1
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]
Timo
January 11, 2024, 1:25pm
3
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
LeXXik
January 11, 2024, 1:39pm
5
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
Timo
January 11, 2024, 1:42pm
6
Aah alright. So I forgot to mention that the script is attached to ‘PositionOneVehicles’ so it will be one less ‘children[0]’ right?
Timo
January 11, 2024, 1:53pm
7
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');
Timo
January 11, 2024, 2:17pm
8
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!
1 Like