[SOLVED] Check if Entity Visible Confusion

Hi, I want to see which balls are visible so I am trying something like:

if(balls[i].model.meshInstance.visible == true)
{
    //do stuff
}

But I just get the error: Cannot read properties of undefined (reading ‘meshInstance’)

Whats wrong here? Each ball entity has a render/asset component.

Thanks

If the Entity has a render component and no model component, then getting the model component (entity.model) is going to return undefined because it doesn’t have a model component.

Because I just followed what someone else did. So I need to do:
balls[i].asset.meshInstance.visible
??

So why are you trying to access the model component? :thinking:

You will need the entity, access the render component of the entity and then the mesh instances from the render component.

Related API docs
https://api.playcanvas.com/classes/Engine.Entity.html#render
https://api.playcanvas.com/classes/Engine.RenderComponent.html#meshInstances

Hmm, forgive me, but I don’t really understand the help very well. I’ve tried each of the following, but I get errors each time:

balls[i].render.meshInstance.visible == false;
balls[i].render.meshInstance[0].visible == false;
balls[i].render.MeshInstances[0].visible == false;

You cannot test .visible member, this is valid only within rendering itself.
The way to do this is using visibleThisFrame - search existing forum post for this.

Okay Thanks. But when I try the following it returns true all the time… whether I’m looking at the mesh or not.

if(balls[i].render.meshInstances[0].visibleThisFrame)

Any idea why?

Ah. Frustrum culling was not enabled on the camera. Thanks!! It works now!

Just to clarify on this post for anyone else that comes across this, neither meshInstance or MeshInstances exist on the the pc.RenderComponent class. The needed property is meshInstances which can be found on the API docs for the render component

2 Likes