I try to implement visibleThisFrame but it's not working on more entities

Here is the code.I add this script to two enties,its work only for last entity i couldn't understand this. 
  let cameIndex = this.app.scene.layers.cameras[0];
   this.app.scene.layers.getLayerByName("World").onPostCull = (cameIndex) => {
      let target = this.entity.render.meshInstances[0];
     this.isVisible = target.visibleThisFrame;
    
   };

I’m kinda confused, is the entity that worked a camera? Are the other entities also cameras? What are you trying to accomplish with .visibleThisFrame?

i have here the sample project.My script work for fonly last sphere entity.I check whit this script if entity visible in camera view.If true than i add the entity dynamicRigidBody

https://launch.playcanvas.com/1245454?debug=true

Ok, so can you actually tell me what you are trying to do in general, so that I can help with a better alternative?

Ok,i told actualy what i want, when entity is visible in camera view than it has rigidbodyDynamic when not deleted rigidbody and disabled it.It’s work with 1 entity.

The render asset may have multiple mesh instances so checking against the first mesh instance may not be enough?

actualy it has 1 mesh instance and 2. entity is just a clone of first.When i duplicate 1. entity in runtime it’s work but not when i duplicate it before i run the scritp.Maybe visibleThisFrame forget first entity and take just last entity.Here is the project maybe it’s help more than just i explain it.
https://playcanvas.com/editor/scene/1245454

onPostCull is not an event (which probably should have been), but only a function. So if you set multiple callbacks on the same layer, only the last one sticks.

1 Like

Ok i thought same like you say.How can i check all entities in same layer?Should i call the fucntion for each entites?

What would be easiest is to tag all the entities you are interested in so you can do a app.root.findByTag call and loop through the list on the onPostCull callback