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 /
avatar image
0
Question by ILikeSoups · May 23, 2011 at 11:16 PM · buttondestroytageventcollide

Perform a Task when Objects are Destroyed

So i have some targets (there are three types, easy medium and hard each type with its own tag) and a cannon that shoots a ball. When i hit the targets they are destroyed and i receive a certain amount of points depending on the target. I want to be able to load a gui button when all targets are destroyed. How could i do 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

4 Replies

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

Answer by save · May 24, 2011 at 12:17 AM

You could also make sure you have all targets in the same empty gameobject (child them on instantiation) and use Transform.childCount. Preferably count the childs right before you destroy a target. This way is always failsafe as it'll always return the correct quantity of existing targets (Wolfram's answer will of course work as well, just make sure that you always keep the numbers straight).

Another way is to use a for-loop and find the objects by name (which is eating more CPU, so make sure this method doesn't run to frequently), this is particularly good if you won't be able to track the number from the start for some reason.

Comment
Add comment · Show 2 · 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 Wolfram · May 24, 2011 at 12:22 AM 0
Share

Ah, much more elegant :o)

avatar image ILikeSoups · Jun 09, 2011 at 02:43 AM 0
Share

i apologize for replying so late thank you meater and wolfram, i have tried both your methods and they work fine but i originally found save's the best. thank you very much for the quick reply save, hehe sorry i couldn't return the favor. Transform.childCount was the perfect choice btw.

avatar image
1

Answer by Joshua · Jun 09, 2011 at 02:47 AM

Way simpler then all of these is to have a counter in the script handling your GUI. If the counter is equal to the number of targets, display the gui. To increment the counter attach to each target

OnDestroy () { //increment counter }

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 save · Jun 09, 2011 at 09:29 AM 0
Share

Yeah simple and clean.

avatar image
0

Answer by Wolfram · May 24, 2011 at 12:02 AM

You should create a var tracking the number of targets, for example in the script that creates your targets. Initialize it to the total number of targets you create. In the Update function, enable the gui button if the number reaches 0.

Then, each time the ball hits a target, subtract 1 from that number, for example in its OnCollisionEnter() function.

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 Meater6 · May 24, 2011 at 12:54 AM

Use these two scripts:

Place this on the target:

 var pointsGiven : float = 10; //Your points here
 var targetCounter : GameObject;
 
 function OnCollisionEnter (thing : Collision) {
     var targetCounterScript = targetCounter.GetComponent("TargetCounter");
 
     if(thing.gameObject.tag == "Cannonball"){
         targetCounterScript.targets -= 1; //subtract targets
         targetCounterScript.score += pointsGiven; //add score
         Destroy(thing.gameObject); //destroys the cannonball
         Destroy(gameObject); //destroys target
     }
 }

And place this on your score GUI. IMPORTANT: make sure to name this script TargetCounter.

 var targets : int = 10;
 var score : float = 0;
 
 function Update () {
     
     if(targets <= 0){
         //button logic
     }
 
     guiText.text = "Score " + score.ToString("f0"); //update score every frame
 }

Add the "Cannonball" tag to the cannonballs. For the button logic part, just have your GUI button turn its button script on.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

UI Toolkit - prevent click through visual element 1 Answer

Problem with collisions 0 Answers

Player tag not being detected by Unity? 2 Answers

Trying to create custom events, best way to attach GameObjects and functions to call? 0 Answers

How to access other Scripts and Components 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