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.

[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, 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.

@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 :slight_smile: