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 androids · Oct 21, 2013 at 05:15 PM · cameraobjectactivevisible

Activate object if visible and Deactivate object if invisible

Hi

I have a Main Camera. And I want to deactivate object if they aren't visible by the Main Camera. And if it is. Then it activate

Someone who can help me?

I tried a lot

Like this.

 function OnBecameInvisible ()
 {
    gameObject.SetActive (false);
 }

Please help. Thanks in advance

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
1
Best Answer

Answer by TheValar · Oct 21, 2013 at 05:47 PM

 Vector3 position = camera.WorldToViewportPoint(transform.position);
 
 if(position.x<0||position.x>1||position.y<0||position.y>1){
 OnBecameInvisible()}

I think that should do it.

p.s. sorry for the horrible formatting I'm using an ancient browser.

EDIT: Ok I feel dumb as I now realize that OnBecameInvisible() is a method of the Renderer class. I'm now confused about what the problem is. Is the method not firing when the object moves out of view?

Comment
Add comment · Show 16 · 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 androids · Oct 21, 2013 at 08:51 PM 0
Share

I copy/paste your code and Unity gives me this errror

 Assets/Scripts/scr_s1ingame/Destroy$$anonymous$$ap.js(1,8): UCE0001: ';' expected. Insert a semicolon at the end.

and

 Assets/Scripts/scr_s1ingame/Destroy$$anonymous$$ap.js(4,20): UCE0001: ';' expected. Insert a semicolon at the end.
avatar image TheValar · Oct 21, 2013 at 09:49 PM 0
Share

It's more like pseudo-code really just to give you an idea of the logic you are after. But off the top of my head try adding a semicolon after the OnBecameInvisible() call and change Vector3 to var since it's in javascript.

avatar image flaviusxvii · Oct 21, 2013 at 09:55 PM 0
Share

@androids Sorry to be blunt but if you don't know what to do about " UCE0001: ';' expected. Insert a semicolon at the end." then I'm afraid you may be a little out of your depth. Try some tutorials. Get more experience program$$anonymous$$g.

avatar image drallim33 · Oct 21, 2013 at 11:29 PM 0
Share

Your example code works when I try it, so deactivating works fine. The problem is, once it has been deactivated, for all intents and purposes it no longer exists. It will not become visible when its position is in the camera frustum since... it's no longer active! And there is no collider to test for using this method. You can reactivate it manually once the camera is pointed at its location again, and once it is again active it should fire off IsBecameVisible, because it just became visible after being reactivated.

What is it exactly that you're trying to do? There should be some other way of achieving our desired results.

avatar image androids · Oct 22, 2013 at 12:46 AM 0
Share

What I exactly try to do is to deactivate (gameObject.SetActive(false);) 50 gameobjects if they're invisible in the $$anonymous$$ain Camera without using Occlusion Culling. If it's visible in the $$anonymous$$ain Camera, it should activate etc.

Show more comments
avatar image
-1

Answer by crazysurge · Oct 22, 2013 at 02:02 AM

if your trying to make an object invisble do this renderer.enabled = false then set a boolean

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

18 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

Related Questions

Know if gameObject is being seen 2 Answers

Como activar una animacion al hacer clic en un objeto (How to activate an animation when you click on an object) 1 Answer

Code running in a previous state with old data after setting objects to active 1 Answer

Make object visible only for camera "X" 1 Answer

Check if specific camera sees object 0 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