- Home /
Renderer.isVisible always returning false
As the question says, I'm always getting false as a return Renderer.IsVisible()
.
In my scene, I have some empty game objects, each one having children with a renderer component.
What I want to do is to delete a parent game object, and its children, when they are not visible.
So, I have this piece of code:
// Delete all platforms that are not visible by the camera.
void DeletePlatform ()
{
List<Transform> toDelete = new List<Transform>();
foreach (Transform platform in m_platforms)
{
if (!IsVisibleFrom(platform, Camera.main))
{
toDelete.Add(platform);
}
}
foreach (Transform delete in toDelete)
{
m_platforms.Remove(delete);
Destroy(delete.gameObject);
}
}
// Auxiliar function to check if a renderer is visible by the camera.
bool IsVisibleFrom(Transform parent, Camera camera)
{
Component[] renderers = parent.gameObject.GetComponentsInChildren<Renderer>();
foreach (Renderer renderer in renderers)
{
Debug.Log(renderer.isVisible);
if (renderer.isVisible)
return true;
}
return false;
}
And this is my scene:
As can be seen, just the top set of platforms are not visible. However, when I call that functions, all of them are destroyed. And didn't get what is happening.
Thank you in advance.
I'm not sure really. They way you're doing it seems a little overly complex. Have you thought about using the void OnBecameInvisible() function? You could put your removal code in there. It would remove the platform as they leave the camera's view.
Something to know if you end up using the void OnBecameInvisible() function is that if your object has to be not visible in your game view AND your scene view in order for OnBecameInvisible() to trigger.
Try out putting the name of the objects that you are checking for a renderer.isVisable on, just to be totally certain that you are looking for it on the right objects. You prob are or I would expect you would get an error, but maybe worth a quick check anyway.
I've already done that, @$$anonymous$$rSoad. It's printing what I expect. I'll try that, @Doc$$anonymous$$cShot.
When I use such a method, @Doc$$anonymous$$cShot, my upper platform (image in the question) is not destroyed. This is the expected behavior? Regarding that just the children of the game object have renderer, not the parent.
Answer by DocMcShot · Oct 21, 2014 at 11:13 PM
@paulaceccon Here's how I do it in a game that I have. I attached the script to the gameObject's that I want to destroy themselves after they leave the camera view. Remember, that in order for you to be able to trigger the OnBecameInvisible() method, the object must NOT be visible at all (this includes the scene view in the editor). If you are wanting to destroy that parent object, you could do something like this:
void OnBecameInvisible()
{
Destroy (gameObject.transform.parent.gameObject);
}
I think this should work :)
Yeah, I tried this here. However, when I deploy I don't have a scene view in editor, so, it will work properly, just considering the game view camera?
Yes. You don't have to have a scene view. I just wanted to make sure that you knew that it does take the scene view camera into consideration also (if you have one). If you run the game in "$$anonymous$$aximize on Play" it works fine for me.
Answer by zharik86 · Oct 17, 2014 at 06:34 AM
I'm not sure, but for function Component[] GetComponentsInChildren(Type t, bool includeInactive = false) is second parametr: includeInactive. Try use it. And second, little change logic in function IsInvisibleFrom():
bool IsVisibleFrom(Transform parent, Camera tpCam) {
bool tpVis = false;
Component[] renderers = parent.gameObject.GetComponentsInChildren<Renderer>(true);
foreach (Renderer renderer in renderers) {
Debug.Log(renderer.isVisible);
tpVis = TpVis || renderer.isVisible;
}
return tpVis;
}
If it's not worked, that is absolute method, when you calculate screen position your elements. And if element behind screen, that element is not visible. I hope that it will help you.
It didn't work. Not even a clue of what I'm doing wrong.. :S