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.
[33787-captura+de+tela+2014-10-16+às+17.23.22.png|33787]
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:
[33788-captura+de+tela+2014-10-16+às+17.23.12.png|33788]
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.
– DocMcShotTry 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.
– MrSoadI've already done that, @MrSoad. It's printing what I expect. I'll try that, @DocMcShot.
– paulacecconWhen I use such a method, @DocMcShot, 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.
– paulaceccon