[SOLVED] My script affects only one object

Hello @smokys! The best way is to give the entities the same tag and use findByTag() in your script. Then you can use the way of @LeXXik to do something.

1 Like

Yup i tried findbytag first but it threw error as it could not find a script attached on those objects, when i used findbyname it was all ok

upper line works and second one doesnt work

image

and throws this error

Are you sure all entities with that tag has a script component?

Put them under a parent and then do this in your script:

var entity = this.app.root.findByName('the parent');

entity.children.forEach(function(node){
// do what ever for each of the children...
});
1 Like

Yes im pretty sure, i even tried just one object with that tag which has a script attached… also it says object is undefined, not the script. i tried also rename the tag and try new but still no success

I have to access a script of those children

var entity = this.app.root.findByName('the parent');

entity.children.forEach(function(node){
   node.script.scriptname.whateverthefunctionis();
});
1 Like

Probably you get the same error because I don’t think it’s because of the findByTag() method.

I think the easiest way was just find by tag but that somehow not working

.findByTag() returns an array of entities. So, you need to iterate over an array of objects and access the .script property of each object.

var entities = this.app.root.findByTag('som-tag');
for (var i = 0; i < entities.length; i++) {
    var entity = entities[i];
    var trap = entity.script.trap01Tronball;
}
1 Like

Ah, my bad. Sorry.

but still throws error

image

That’s another error @smokys. Double check what you are doing. The last three lines are outside the for loop.

yup i tried to fix, and put those lines inside the loop but i tried to output something from there and it lookts like it ignores the onCollisionStart or the loop

image

Yes because it has to be result.other instead of just other.

It was working before even without result, anyway i tried to put result in and still not it

I will look at your project when I have some time.

I took a look and I see you mix up the methods of triggers and colliders. You use the initialize of a trigger and the rest of your code of a collider. Check the manual to make sure you’re doing it right.

https://developer.playcanvas.com/en/tutorials/collision-and-triggers

SOLVED, special thanks to @Albertos

1 Like