Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Harry64 · Aug 11, 2013 at 02:07 PM · parentrendererchildrenisvisible

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harry64 · Aug 11, 2013 at 11:10 PM 0
Share

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++;
 }
avatar image robertbu · Aug 12, 2013 at 02:51 PM 0
Share

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[];
 }
avatar image Harry64 · Aug 12, 2013 at 06:16 PM 0
Share

thanks for the answer ^^

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Deactivate Specific Children In Partent 1 Answer

GetComponent searches an objects child instead of the object 2 Answers

How To Get List of Child Game Objects 14 Answers

accessing all of a colliders parents children 1 Answer

onTriggerEnter is unclear to me... 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges