- Home /
check if children are visible, if not destroy parent?
how do I check with renderer.isVisible if the children are visible?
I have a empty gameobject and there should be this script which should check if the children of this empty are visible to the playerCamera. if no one of the children is visible then it should destroy the parent so that the children are also gone.
it should be like if I break a object and the debris should not go away until the player dont look at them.
the problem I have is I can check if the parent is still visible but the parent is a empty with no renderer...
Answer by robertbu · Aug 11, 2013 at 04:28 PM
Here the code to detect if any child is being looked at. Note I'm assuming here that children are not added/removed during game play.
#pragma strict
var renderers : Component[];
function Start() {
renderers = gameObject.GetComponentsInChildren(Renderer);
Debug.Log(renderers.Length);
}
function Update() {
Debug.Log(IsVisible());
}
function IsVisible() : boolean {
for (var rend : Renderer in renderers) {
if (rend.isVisible)
return true;
}
return false;
}
Note the Renderer.isVisible flag returns 'true' if any camera see the object. In the editor, that includes the Scene camera. So to test this code in the editor, you need to point the scene camera away from the objects you are testing.
ok I put it in to see if it works and it does. but I have a question and also I found a little bug in this script... ^^; sorry but I just want to know about the way you wrote the script because I am started learning unity java last year December and so I do not know all about it.
first the bugs:
function IsVisible() : boolean {
for (var rend : Renderer in renderers) {
if (rend.isVisible)
return true;
}
return false;
I found out that in the first frame the script says that the IsVisible boolean = false.... can I change it somehow that its true in the first frame?
and I get somehow the warning Implicit downcast from 'UnityEngine.Component' to 'UnityEngine.Renderer'.
I found in the internet a solution and writed it into the code but I dont understand how this works... here is the solution:
var renderers : Renderer[];
renderers = gameObject.GetComponentsInChildren.<Renderer>();
when I write this var and ins$$anonymous$$d of your (Renderer) this .<>(Renderer)then this warning is no longer there. but why?
now thanks to your help I was able to create it like I wanted it. but maybe you have time to respond to my quesstions so that I can get better. thanks ^^
#pragma strict
var renderers : Renderer[];
var destroyAfterFrames : int = 200;
var framesNotVisible : int;
function Start()
{
renderers = gameObject.GetComponentsInChildren.<Renderer>();
}
function Update()
{
IsVisible();
if(framesNotVisible >= destroyAfterFrames)
Destroy(gameObject);
}
function IsVisible()
{
for (var rend: Renderer in renderers)
{
if (rend.isVisible)
{
framesNotVisible = 0;
return;
}
}
framesNotVisible++;
}
I don't know how to change the value if isVisible to true for the first frame. As a hack you delay starting the checking for a short period of time.
The form of GetCompontentInChildren with the is the generic version. You can think of it as generating a special function just for that type...as if you had a function called 'GetComponentInChildrenRenderers()' or 'GetRenderersInChildren()'. Rather than return an array of the base type 'Component', it will return an array of Renderers. You can substitute any component within the , including scripts you've written. You could also have fixed the warning by casting the return of GetComponentsInchildren().
var renderers : Renderer[];
function Start() {
renderers = gameObject.GetComponentsInChildren(Renderer) as Renderer[];
}