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 DYV · Aug 09, 2019 at 04:41 PM · array of gameobjectsc# tutorialdestroyed

how to check if all object in my array are destroyed (C#)

Hello Could someone help me? I want to check if all object in my array are destroyed and when all of them are destroyed do something. I saw the answer for JS but I need C#. Please :)

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

3 Replies

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

Answer by DYV · Aug 11, 2019 at 09:30 AM

I have found the easiest way for my special case :)

  if (GameObject.FindGameObjectWithTag("Enemy") == null)


So there was no need in array at all

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 I_Am_Err00r · Aug 09, 2019 at 04:49 PM

Here you go:

 if(whateverArrayYouAreChecking.Length < 1)
         {
             DomSomething();
         }
Comment
Add comment · Show 10 · 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 DYV · Aug 09, 2019 at 05:27 PM 0
Share

Thank you for the answer but it does not work for me. For example I have two object in my array, when I destroy both of them nothing is happened

if ($$anonymous$$iBosses.Length < 1) { gameController.BossIsDead(); Destroy(gameObject); }

avatar image I_Am_Err00r DYV · Aug 09, 2019 at 06:03 PM 0
Share

Ok, I see what is going on here, is there any reason why you are using an Array? Do you need to go that route? Could you do a List ins$$anonymous$$d? It seems like an 'int' might even be a better way to go about this:

 int $$anonymous$$iBossCount = however$$anonymous$$iniBossesYouHave;
 
 //call this right before you destroy a $$anonymous$$iBoss
     $$anonymous$$iBossCount--;
     if($$anonymous$$iBossCount < 1)
     {
     DoSomething();
     }

avatar image DYV DYV · Aug 09, 2019 at 06:22 PM 0
Share

this method is interesting but I would not like to add to existing scripts something. I mean "$$anonymous$$iBossCount--;" This object is $$anonymous$$iBoss only in special case, usually that is just Enemy.

avatar image I_Am_Err00r DYV · Aug 09, 2019 at 06:27 PM 1
Share

This line:

I would not like to add to existing scripts something

You want help writing code, but you don't want to change a script; love it!

Good luck man!

Show more comments
avatar image I_Am_Err00r · Aug 09, 2019 at 05:29 PM 0
Share

Do you want to mark this as the correct answer? It'd be a lot cooler if you did!

avatar image
0

Answer by sacredgeometry · Aug 09, 2019 at 06:25 PM

You could try an ObservableCollection with a test for the size of the array.

Note: Needs .Net 4.+ enabled in your player settings  


Video


Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Collections.ObjectModel;
 using System.Collections.Specialized;
 
 public class DoWork : MonoBehaviour {
 
     public ObservableCollection<int> Ids = new ObservableCollection<int>
     {
         0, 1, 2, 3, 4, 5
     };
 
     // Use this for initialization
     void Start () {
         Ids.CollectionChanged += OnCollectionChanged;
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             if(Ids.Count > 0)
             {
                 Ids.RemoveAt(Ids.Count-1);
                 Debug.Log($"ArraySize: {Ids.Count}");
             }
         }
     }
 
     void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
     {
         if(Ids.Count == 0)
         {
             DoSomething();
         }
     }
 
     void DoSomething()
     {
         Debug.Log("Empty Array");
     }
 }
 

EDIT:

The solution you requested in the comments is really terrible code/ badly written, poorly optimised but if you were going down that route.

Just remove items from the collection when you destroy them and then check the collection size instead. i.e.

 List<MyObject> MyObjects = new List<MyObject>();
 
 void Update() 
 {
     if(MyObjects.Count == 0) DoSomething();
 }
 
 void Destroy(index: int) {
     if(index < MyObjects.Count)
     {
         MyObjects.RemoveAt(index);
     }
 }
 
 void  DoSomething() 
 {
     // CODE GOES HERE
 }
 

Note that this is not as efficient as the first example code as you are constantly checking for the state of the collection on every frame.

Comment
Add comment · Show 5 · 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 DYV · Aug 09, 2019 at 06:44 PM 0
Share

Thank you but it's too complicated for me. I just want to find the same solution link text only with C#

avatar image sacredgeometry DYV · Aug 09, 2019 at 06:53 PM 0
Share

I can make a video if it helps. Its not as complicated as it first looks.

avatar image sacredgeometry · Aug 09, 2019 at 07:09 PM 0
Share

I have uploaded a video. See above

avatar image DYV sacredgeometry · Aug 10, 2019 at 12:12 PM 1
Share

I really appreciate your answer and especially video, I hope someone will use your method, I was needed just simple solution

avatar image sacredgeometry DYV · Aug 10, 2019 at 12:18 PM 0
Share

$$anonymous$$aybe you will revisit it in the future when you are more comfortable with c#

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

111 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

Related Questions

Application.LoadLevel problem 2 Answers

Button activating before clicking 2 Answers

how can i know about the libraries of c# in unity 1 Answer

No Monobehaviour scripts in the file 2 Answers

move from left to right and right to left 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