Snippet3D ObjectManagement Multiple Similar Objects

Managing Multiple Similar Objects


multiple-similar-objects_result

When managing multiple objects with the same or similar logic, you can name the objects sequentially as shown in the picture below and use a for loop to easily retrieve them.


multiple-similar-objects_explorer

let objs = []; // An array to manage the objects
 
// Use a for loop to retrieve objects with sequential names
for(let i=1; i<=5; i++){
    objs.push(WORLD.getObject("obj" + i)); 
    
    // Allow each retrieved object to perform collision detection with onCollide
    objs[i-1].onCollide(PLAYER, function() {
        objs[i-1].kill()
    })
}