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 Lukas-Barbarossa · Sep 12, 2012 at 06:35 PM · rigidbody

How do I know all objects have been destroyed?

Hello

I have three targets in a scene made of cubes, called target1, target2, and target3. They are destroyed on collision with a RigidBody.

How do I know when these three objects are gone so I can call my 'Game Over' state?. They aren't prefabs or dragged onto anything in the inspector right now, and their only component is the code to destroy themselves on collision.

Thanks in advance.

UPDATE

Thanks to Screenhog's answer below, I was able to stop my counter counting up when all the targets are destroyed. Here's the final code for reference.

 private var timeElapsed:float = 0f;
 var targets:Target[];
 
 
 
 function Update(){
     if (targets.length > 0){
         timeElapsed += Time.deltaTime;
     }
     else {
         print("Clock has stopped");
     }
     
     targets = FindObjectsOfType(Target) as Target[];
     print("Target length is " +targets.length);
 }

This trick of using a unique component to identify a group of objects and then using FindObjectsOfType is another really nice piece of the Unity puzzle slotting into place.

Thanks for the help.

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 Screenhog · Sep 12, 2012 at 07:38 PM

I would find a way, through scripting, to populate them in an array (giving them all a particular script component and using FindObjectsOfType is a good way). When one is destroyed, remove it from the array, and then do a test to see if any objects are still in the array. If the length of the array is 0, then it's Game Over.

That's the overview... is that enough to help, or do you need more detail?

Comment
Add comment · Show 4 · 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 Lukas-Barbarossa · Sep 13, 2012 at 08:45 AM 0
Share

Thanks Screenhog. Thats a really good challenge to get my head around (bit of a n00b). And the tip of using a component to round up a bunch of similar objects is a really handy insight. I'll give it a go now and let you know how it goes.

avatar image Lukas-Barbarossa · Sep 13, 2012 at 09:17 AM 0
Share

As you can see above, I got it working. Thanks again. It's always better to be pointed in the right direction and then find the answer, rather than to be spoon-fed. I feel like I really learned something.

I wasn't sure what the forum protocol was for including the correct code, whether it should be a new answer or an update to the original question, so I took a punt and edited the OP.... didn't want to s$$anonymous$$l you answer glory :)

avatar image Psymon · Sep 13, 2012 at 09:21 AM 0
Share

Be careful FindObjectsOfType is a very slow procedure and you call it every frame. You should populate the array when you create a target ins$$anonymous$$d of serching them after.

avatar image Lukas-Barbarossa · Sep 13, 2012 at 09:46 AM 0
Share

I know what you mean, it is kind of hacky. Ideally, I would delete the target from the array upon destruction, then just check for length on Update(). Unfortunately, I haven't got my head around how to access the timer script (where the array is declared) from the target so it pops itself out of the array bfore destruction.

In fact, getting a script to talk to a script on another object is a cause of about 75% of my confusion with Unity. I can't wait for that piece to pop into place. Thanks for the comment though, I will get better :)

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

How to make gameobjects fall down faster with each update ? 1 Answer

Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer

Addforce to ball in camera position 0 Answers

AddForce doesn't work when used with velocity 3 Answers

How can I rotate around with a Rigidbody while respecting physics? 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