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 Rydrako · Dec 08, 2012 at 03:26 AM · transformarraynulliffor

How do I check if all objects in an array are destroyed?

Hai everyone! So I was wondering if I could use an array to check if all the gameObjects in that array are destroyed. This is what I tried doing:

 for(var i=0;i<Turrets3.Length;i++)
         {
                 if(Turrets3[i] == null)
                 {
                         Start3 = true;
                 }
         }

But that made it if I only destroyed one of the enemies attached to the array I have. I don't know if I'm not using the for() thing wrong or if there's another way to check it. ( NOTE: I have never used a for() thing before ) As always your help is appreciated! :D

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

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

Answer by Rydrako · Dec 08, 2012 at 03:41 PM

Ok I guess I'm not good for loops but I used JeffShock's concept. So I did something like this:

 var i3 = 0;
 var aliveCount3 = 0;
 
 function Start ()
 {
                 aliveCount3 = Turrets3.Length;
 }
 
 function Update ()
 {
 
 if(Turrets3[i3] == null)
         {
                 aliveCount3 -= 1;
                 i3++;
         }
                 
         if(aliveCount3==0)
         {
                 Start3 = true;
         }
 }

It works great!

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
1

Answer by JeffShock · Dec 08, 2012 at 04:07 AM

You could do something like this:

 int aliveCount = 0;
 for(var i=0;i<Turrets3.Length;i++)
 {
     if(Turrets3[i] != null)
     {
          aliveCount++
     }
 }

This basically counts how many elements are still alive. If aliveCount == 0 at the end of the loop, you know that all of the elements are null or dead.

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 aldonaletto · Dec 08, 2012 at 04:19 AM 0
Share

Another possibility:

 var Start3: boolean;

 Start3 = true; // initialize Start3 to true
 for (var gameObj: GameObject in Turrets3){
   if (gameObj) Start3 = false; // any turret alive turns Start3 to false
 }
avatar image Rydrako · Dec 08, 2012 at 03:30 PM 0
Share

I tried both of you guy's answers but it still calls Start3 if only one element is destroyed. $$anonymous$$aybe I'm not doing something right...

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

13 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

Related Questions

Read Array Inside Array 0 Answers

Accessing boolean properties from array of transforms 1 Answer

Array index is out of range 1 Answer

Wrong if command 3 Answers

Problem with nested for loops 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