TypeScript: Acting upon all children of a group

I am creating an Elevator using TypeScript. The elevator car (the part that moves up and down) is comprised of many objects, including (but not limited to) the inner doors, the walls, the control panel, the overhead light, and more.

I would prefer to declare this set of items in my Scene rather than in my code.
So, I created a Group to house all the items that I want. This group now has a unique item ID, so I’d like to select all child items of the group and make them move.

Here, this.carGroup is my group of items.

this.carGroup.children.toArray().forEach(item => {
    item.transition.moveBy(new Vector3(0, newHeight - oldHeight, 0), Math.abs(newHeight - oldHeight) / this.speeds[0]);
});

However, it appears that children might not be the correct keyword to get a group item’s children. This is evident because my browser console outputted an error reading:

TypeError: Cannot read properties of undefined (reading 0)
classes.js:25 at eval (play/EV.js:94:128)
classes.js:25 at Array.forEach (<anonymous>)
classes.js:25 at Elevator.upFloor (play/EV.js:93:46)

So how can I identify and act upon a set of multiple objects at once? Is a group the correct way?
Keep in mind that I would prefer to define this group of objects in my Scene rather than in my code. Listing all item object IDs of each object individually is to be avoided.

It appears that the problem was not within the forEach loop that I had. The loop was correct and the error was out-of scope but the error message tripped me up.

The error was that this.speeds was an empty list, so reading [0] from it caused an index out of bounds exception.

1 Like