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
3
Question by agentsmith · Jul 26, 2010 at 08:38 PM · optimizationonbecameinvisibleonbecamevisible

How do I use OnBecameVisible and OnBecameInvisible?

Hi all,

Unity's optimization page suggests using the OnBecameVisible/OnBecameInvisible functions.

I have a cube that is outside the camera's view frustum (its about 30 meters behind the player) and I have a script attached to the cube with the following code:

function OnBecameVisible () {

print("VISIBLE");

}

function OnBecameInvisible () {

print("INVISIBLE!");

}

I'd like to have it print a message when the cube is in the camera's frustum and when it leaves the camera's frustum. Unity's documentation reads, "This message is sent to all scripts attached to the renderer." The cube does have a mesh renderer and when the scene starts I receive a "VISIBLE" (even though I start not facing the cube) and then when I face the cube and turn away (180' or an about face) it doesn't give me the "INVISIBLE" message. As I said I'm ~30 meters away so I'm confused why this is happening. Also it will only give me a "VISIBLE" message on start up. Looking away and back at the cube produces no more messages. Any suggestions? Thanks!

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

2 Replies

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

Answer by Mike 3 · Jul 26, 2010 at 08:43 PM

Make sure the editor/scene camera isn't pointing at the cube. It counts for rendering it too

Otherwise, your code looks fine

Comment
Add comment · Show 6 · 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 agentsmith · Jul 26, 2010 at 08:45 PM 0
Share

Thank you $$anonymous$$ike, that did the trick!

avatar image robertofantasy · Oct 08, 2012 at 01:43 PM -1
Share

i have another problem i want when the object is visible move that by transform.Translate() but because it is not in update method it just move a bit and then not moving

avatar image robertofantasy · Oct 08, 2012 at 01:44 PM 0
Share

hi i have exact same problem but none of the trick you friend said not work it always print the message

i dont understand that you say camera pointing the object

but if you mean that camera not show the object in editor at first i do it and its not work

i even disable cast and recieve shadow please help me

avatar image igdeman robertofantasy · Apr 09, 2019 at 11:32 AM 0
Share

$$anonymous$$aybe you have attached your script on to object without any renderer...

avatar image robertofantasy · Oct 08, 2012 at 02:49 PM -1
Share

please help me

avatar image Igorotak · Dec 05, 2017 at 08:41 PM 0
Share

Im having trouble with $$anonymous$$e. It can detect the object even though its behind walls and whatknots. I cant use raycast since the rooms I use are technically one object with flipped faces.

avatar image
2

Answer by Waz · Aug 21, 2010 at 05:19 AM

Another gotcha: it seems dynamic shadows also activate "visibility" (presumably because they are projected). So you would need to turn off "Cast Shadows" on the cube too.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to reinstate a disabled object when becomes visible? 1 Answer

OnBecameVisible and OnBecameInvisible with LOD group. 2 Answers

Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers

why the function OnBecameVisible and OnBecameInvisible doesn't work in canvas renderer ? 1 Answer

How do I stop a moving object when the Camera sees it 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