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 /
avatar image
0
Question by EvanCheddar · Jan 31, 2018 at 10:37 PM · score systemcountergame objecttimer-scriptdestory

destroy all game objects that are touching the same collider?

Okay let me set this up for you guys. I have a bowl and I have tennis balls that slowly fall into the bowl.
I have scripts set up on the bowl and the balls so that it can count how many tennis balls have entered the bowl. I want to now create a script that every 30 seconds, the tennis balls get counted and if there are 8 or less balls in the bowl, destroy all balls (essentially empty the bowl) BUT if there are 9 or MORE balls in the bowl, it's game over (bowl overflow)

here's the scripts that I have attached to the Ball:

public int Balltocount;

 void OnCollisionEnter(Collision other)
 {
     if (other.gameObject.tag == "Bowl") 
     {
         other.gameObject.GetComponent<BallCounter>().CountBalls(Balltocount);
     
     }
 }

} And this is what I have on the BOWL:

public int Counter;

 public void CountBalls(int howmany)
 {
     Counter -= howmany;
 }

}

this works fine and is able to count the balls colliding in the bowl, its just the next step that i mentioned above that i cant figure out. Thanks to anyone who takes a look at 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

1 Reply

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

Answer by Obsessi0n · Feb 01, 2018 at 08:38 PM

This should work atleast the logic is correct.

 public class ballcounter : MonoBehaviour {
     public int totalballs =0;
     private GameObject bola;
     private GameObject[] allballs;
     private int i = 0;
     public void CountBalls(GameObject bola)
     {
         totalballs++;
         allballs[i] = bola;
         i++;
     }
 
     private void Start()
     {
         allballs = new GameObject[100];
         StartCoroutine(Destroy());
     }
 
 
     IEnumerator Destroy()
     {
 
         while (true)
         {
             yield return new WaitForSeconds(5f);
             if (totalballs > 8)
             {
                 //gameover
             }
             else if (totalballs <= 8 && totalballs > 0)
             {
                 for(int j =0;j< allballs.Length; j++)
                 {
                     if(allballs[j] != null)
                     {
                         Destroy(allballs[j].gameObject);
                     }
                     
                     
                 }
                 i = 0;
                 totalballs = 0;
             }
         }    
             
         
 
     }
 }
     public class ball : MonoBehaviour {
     
         
     
         void OnCollisionEnter(Collision other)
         {
             if (other.gameObject.tag == "Bowl")
             {
                 other.gameObject.GetComponent<ballcounter>().CountBalls(this.gameObject);
     
                 Destroy(this.gameObject.GetComponent<ball>());
             }
         }
     }





Comment
Add comment · Show 7 · 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 Obsessi0n · Feb 01, 2018 at 08:53 PM 0
Share

Corrected syntax and tried on my project and worked just fine.

avatar image EvanCheddar · Feb 01, 2018 at 10:16 PM 0
Share

Just want to say thank you for this answer! Sorry if I'm a little slow, I'm new to all of this. I'm still getting a NullReferenceException:Object reference not set to an instance an object on Line 34. I have no idea why this is happening.

avatar image Obsessi0n EvanCheddar · Feb 01, 2018 at 10:18 PM 0
Share

hmm Im not getting it in my project I will look into it later. No problem I am also still learning :)

avatar image Obsessi0n · Feb 01, 2018 at 10:39 PM 0
Share

Copy the code again. I changed a few things it should no longer give NullReferenceException

avatar image EvanCheddar · Feb 01, 2018 at 11:18 PM 0
Share

It works and quite brilliantly at that. Thanks again, I've learned a lot and I sincerely appreciate your time and effort!

avatar image EvanCheddar · Feb 02, 2018 at 07:34 PM 0
Share

Hey man, if you would be so kind, I have one more quick question: I have a gameobject "grenade" in my game that gets thrown in the Bowl and destroys all the balls in the bowl. I would like this grenade to also set the counter back to "0" since all the balls are out of the bowl. I've tried adding OnCollisionEnter () into the script, I've also tried creating a new function but nothing seems to reset the counter back to "0" after the grenade goes off. I really hate to bother you but you're my only hope at this point! thank you sir.

avatar image Obsessi0n EvanCheddar · Feb 02, 2018 at 07:49 PM 0
Share

Can you create a new question with your code? I will look into it and try to help.

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

124 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 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 do you add +1 to a score counter after a destroy script (C#)? 1 Answer

How do I get a "counter" to reset back to "0" 2 Answers

How do I switch scenes with countdown timer and Score at the same time 0 Answers

Score counter system 2nd script and ball a little problem 1 Answer

How do I stop a scene from switching from switch before the Countdown timer reaches zero 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