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
2
Question by NickelBuckle9 · Oct 29, 2012 at 04:43 AM · screencloneviewcountvisible

How to count the number of clones within view?

Hello!

I have a random ghost generate when triggered, using Instantiate. They appear for a few seconds before destroying themselves. While they're active, the player has to try and take a picture with as many of them on the screen at once as possible.

Everything in my code is working fine up until this point, and I'm just trying to figure out how to make a variable count the number of ghosts within the view of the camera. I looked at a few other similar questions, and a loop with "isVisible" counting the renderers is working, but the problem it counts each piece of the rendered items, which isn't good for my ghosts which have multiple parts.

It also counts ALL renderers, not just those on the ghosts.

How would I count the number of ghosts (clones from Instantiate) that are on the screen?

Here's my code for the counter:

 if(Input.GetMouseButtonDown(2) && RaisedCamera == true){
             
         Renderer[] renderers = (Renderer[])(FindObjectsOfType(typeof(Renderer)));
         Count = 0;
             foreach(Renderer i in renderers) {
                 if(i.isVisible) {
                     Count += 1F;
                 }
             }
Comment
Add comment · Show 1
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 Fattie · Oct 29, 2012 at 07:47 AM 0
Share

it's possible you will have to carefully keep track of the ghosts in a pool

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

then, just count them in that pool !

1 Reply

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

Answer by aldonaletto · Oct 29, 2012 at 11:19 AM

You could use some specific tag for the ghosts (like Ghost, for instance), grab them all in an array and check their isVisible properties:

 if(Input.GetMouseButtonDown(2) && RaisedCamera == true){
    GameObject[] ghosts = GameObject.FindGameObjectsWithTag("Ghost");
    Count = 0;
    foreach(GameObject ghost in ghosts) {
      if (ghost.renderer.isVisible) {
        Count++;
      }
    }
    ...

But be aware that isVisible simply shows whether the object's bounding volume is inside or touching the view frustum of any camera. If a ghost is out of view but its bounding box touches the view frustum, isVisible returns true. The same applies for a ghost hidden by a wall, or visible in a second camera (if any).

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 NickelBuckle9 · Oct 29, 2012 at 03:45 PM 0
Share

Ahh! That works perfectly! Thank you so much!

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

11 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

Related Questions

Fixed width, relative height, on different aspect ratio screen? 4 Answers

Setting gameobject position by a percentage of screen 1 Answer

Change resolution, but not screen size. 1 Answer

Screen to view point? 2 Answers

Screen position out of view frustum error? 2 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