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 paulaceccon · Oct 16, 2014 at 08:27 PM · camerarenderervisible

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.

alt text

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:

alt text

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.

captura de tela 2014-10-16 às 17.23.22.png (10.2 kB)
captura de tela 2014-10-16 às 17.23.12.png (18.2 kB)
Comment
Add comment · Show 4
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 DocMcShot · Oct 16, 2014 at 11:07 PM 1
Share

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.

avatar image MrSoad · Oct 16, 2014 at 11:26 PM 0
Share

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.

avatar image paulaceccon · Oct 21, 2014 at 04:24 PM 0
Share

I've already done that, @$$anonymous$$rSoad. It's printing what I expect. I'll try that, @Doc$$anonymous$$cShot.

avatar image paulaceccon · Oct 21, 2014 at 04:34 PM 0
Share

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.

2 Replies

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

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 :)

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 paulaceccon · Oct 22, 2014 at 11:44 AM 0
Share

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?

avatar image DocMcShot · Oct 22, 2014 at 02:10 PM 0
Share

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.

avatar image paulaceccon · Oct 22, 2014 at 06:54 PM 0
Share

Thank you.

avatar image
0

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.

Comment
Add comment · Show 1 · 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 paulaceccon · Oct 21, 2014 at 04:24 PM 0
Share

It didn't work. Not even a clue of what I'm doing wrong.. :S

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

30 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 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 avatar image avatar image

Related Questions

Problem is "renderer.isVisible" 3 Answers

How to get a ParticleSystem to play when you can't see it's origin point. 0 Answers

Only show GameObject if at least 1 pixel is rendered 0 Answers

Help! Skinned mesh render not visible!! 3 Answers

Render if Object is behind obstacle? 1 Answer


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