Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jan 31, 2018 at 10:28 PM by EvanCheddar.
avatar image
0
Question by EvanCheddar · Jan 29, 2018 at 05:16 PM · timerareagameoverzonepoints-counter

How can I count the number of objects touching a collider and then destroy them?

Hello all,

I have a simple game where tennis balls fall down from off the screen and into a bucket. The player is on the side shooting projectiles at the balls trying to knock them out of the bucket's path. I want to create a script where every 30 seconds the bucket gets "emptied" only if 7 or less balls are allowed in, if at the 30 second mark 8 or more balls are allowed in, then the game ends, you lose and its game over. It's fairly simple. I'm fairly new to Unity and also to c# so the simpler the better.
Thank you so much for taking the time to read this!

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

  • Sort: 
avatar image
0

Answer by ShimonKinkajoo · Jan 29, 2018 at 05:46 PM

A simple solution would be to trigger counts on collisions. Your bucket already has colliders on it i guess, so just look at the collisions, take the collider that is not the bucket, and see if it's in your "Scored" list. if not, add it. The problem is a ball can bounce of the bucket and not get in. so you can place a horizontal plane collider in the middle of the bucket, so a ball that touches it, is for sure stays in the bucket.

Another approach is to go over all the balls, and see which ones occupy a specific x,y,z limitations (the position and size of the bucket give or take).

Both solutions have different problems, and advantages. But this should give you a way to think about this.

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

Answer by Obsessi0n · Jan 29, 2018 at 06:57 PM

There are multiple ways to do this, one could be: Create a script that every 30 seconds checks all the balls ingame and check how many are colliding with the bucket if there are the desired number to empty then empty it. For exemple:

  • 1- Add a tag "ball" to the ball prefab

  • 2- Every 30 seconds (with the IEnumerator function) check all objects ingame with tag ball and if they are colliding with the bucket

  • 3- If there are more than X empty bucket.

Comment
Add comment · Show 8 · 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 EvanCheddar · Jan 29, 2018 at 08:11 PM 0
Share

Not sure how to use the IEnumerator function, is there any way you can explain this or show me further?

avatar image Obsessi0n EvanCheddar · Jan 29, 2018 at 08:26 PM 0
Share
 void Start()
     {
         StartCoroutine(Example());
     }
 
     IEnumerator Example()
     {
         while (true)
         {
         yield return new WaitForSecondsRealtime(30f);
         #Check the bucket here
          }
     }
     

This will execute a while cycle every 30 seconds

avatar image EvanCheddar Obsessi0n · Jan 29, 2018 at 09:59 PM 0
Share

This won't work because not all of the balls will be touching the bucket, some will be on top of each other, is there a way to check for the balls at a certain location (the buckets transform)?

Show more comments

Follow this Question

Answers Answers and Comments

77 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 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 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

How can I change the controls of a game in a certain area? 2 Answers

Audio Zone, that covers a Square Area? 0 Answers

Getting a timer to go to 0 1 Answer

Capture Zone Timer 0 Answers

How do I add my Timer to my Score system in C# ? 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