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 Gillissie · Mar 10, 2011 at 05:21 AM · rendererisvisible

renderer.isVisible problem on prefab

I am trying to determine if a prefab is out of camera view so I can destroy it. However the prefab itself doesn't appear to have a render object because renderer.isVisible causes errors. So I tried this, but myRenderer.isVisible is always returning true no matter what. The prefab is named Bat, and the model inside the prefab is also named Bat. I don't even know if GameObject.Find searches only the current hierarchy or the entire scene. The docs don't say.

function Start() { var obj:GameObject = GameObject.Find("Bat");

 myRenderer = obj.renderer;

 bornTime = Time.time;

}

function Update () { // Randomly change direction. transform.Rotate(Random.Range(-15,16),Random.Range(-5,6),0);

 // Fly forward.
 transform.Translate(0,0,speed);

 // If I've been alive for more than 5 seconds and I'm off the screen, delete me since I could possibly never return to view.
 if (Time.time - bornTime > 5 && myRenderer.isVisible == false)
 {
     Destroy(gameObject);
 }

}

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

3 Replies

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

Answer by Molix · Mar 10, 2011 at 05:50 AM

In the script you've posted, I don't see a problem with your usage of isVisible.

Find searches the whole scene. Based on the Update comments, presumably this script goes in the Bat, in which case don't bother with Find.

Note that the 'renderer' getter will only look for a renderer in the same object, so if there is a renderer in a child of the object, that will not find it. Instead you could use:

myRenderer = GetComponentInChildren<Renderer>();
Comment
Add comment · Show 5 · 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 Molix · Mar 10, 2011 at 05:50 AM 0
Share

Damn I'm slow :)

avatar image Gillissie · Mar 10, 2011 at 06:20 AM 0
Share

Thanks for the tip on the function. I tried it out and there are no errors, but it still always returns true for isVisible. I know the bat is flying out of camera view.

avatar image Molix · Mar 10, 2011 at 03:24 PM 0
Share

I think if you have the object visible in the scene view, that counts as isVisible, e.g. try closing/hiding the scene view while you play. I did a quick test and that seemed to make the difference.

avatar image Molix · Mar 10, 2011 at 03:25 PM 1
Share

Oh, and sometimes it has to fly fairly far out of view, e.g. the docs mention that it is still 'visible' even if it is off screen but is casting shadows, etc.

avatar image Gillissie · Mar 10, 2011 at 04:27 PM 0
Share

Yes, this is what is happening. If I scroll the view away from the action while it's running, false is returned.

avatar image
0

Answer by DaveA · Mar 10, 2011 at 05:36 AM

Is this script attached to a Bat? If so, don't find at all. Just use 'renderer.isVisible' and it will tell you if 'this' prefab instance is visible.

If this script is attached to some other object, then Find will return the first Bat it finds. Yes, I'm pretty sure it checks the whole scene. If your Bat is at the top level, you could use /Bat to have it just check the top level. What I've done, to check the 'current' hiearchy (this object and it's children) is something like GameObject.Find (name+"/Bat");

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 Gillissie · Mar 10, 2011 at 05:52 AM 0
Share

Yes, it is attached to the bat prefab. I also expected the renderer object to exist, but it doesn't at that level. I get errors about not having a renderer at runtime. So I figured it must be because it's attached to the prefab container, which doesn't have a renderer, so I try to find the component within the prefab that actually has the renderer.

avatar image Gillissie · Mar 10, 2011 at 05:55 AM 0
Share

This guy is having the same fundamental issue as me: http://answers.unity3d.com/questions/6516/how-to-fade-out-an-object-with-no-renderer-and-how-is-that-even-possible

avatar image DaveA · Mar 10, 2011 at 07:47 AM 0
Share

GetComponentInChildren, as $$anonymous$$olix has pointed out

avatar image
0

Answer by Mike 3 · Mar 10, 2011 at 06:40 AM

Besides using GetComponentInChildren to get the correct renderer, make sure that the scene (editor) camera isn't pointing at the object you're checking visibility for.

The editor's camera counts towards returning true for isVisible, very easy to slip up with it

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 rexxthunder · Aug 14, 2013 at 03:02 AM 0
Share

Wow! Your right. What a strange choice on Unity's part.

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

1 Person is following this question.

avatar image

Related Questions

renderer.is(Completely)Visible? 1 Answer

check if children are visible, if not destroy parent? 1 Answer

Render.IsVisible returning false for one frame 2 Answers

C# Renderer.IsVisible counts when editor camera sees object 0 Answers

Changing two different objects renderer colour 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