Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 HajiyevEl · Jun 23, 2020 at 12:10 PM · loop

Checking if all gameobjects in list are inactive

I need to check a list and DO SOMETHING once they ALL become INACTIVE, and DO NOTHING if even ONE of them is ACTIVE. By active i mean active in hierarchy (gameobject not disabled)

 private List<Ball> balls;
 
 public void SetUpBalls()
         {
             balls = new List<Ball>();
         }




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 ShadoX · Jun 23, 2020 at 02:24 PM

The only way I could think of would be to iterate over the list and check each entry, but that would be very costly, if you would want to do that all the time. An example would be the following method:

 bool AreAllBallsInactive(){
     foreach(Ball ball in balls) {
          if(ball.activeSelf) {
             return false;
          }
     }
     return true;
 }

that uses ActiveSelf to check if a ball is active or not

The problem with iterating over everything is that it wouldn't be very efficient, especially if you need to check it often or have a lot of entries in the list.

A better way would be to just call it once when you need to know it and that's it. You could also use LinQ to essentially do the same, like for example:

 List<Ball> activeBalls = balls.Where(ball => ball.activeSelf == true);
 if(activeBalls.Count > 0) {
 // there are some active balls
 }
 //or alternatively just check
 activeBalls.Any(); //if it's true, it means that the activeBalls list contains something and thus not all balls are inActive

though it basically amounts to the same thing.. iterating over everything to figure out if any of them are active

Just keep in mind that iterating that balls list isn't something you should do super often (by that I mean, in an Update call)

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 HajiyevEl · Jun 23, 2020 at 03:26 PM 0
Share

Thanks for answer, but i already did it myself. Used Linq.

         public bool AllBallsDisabled
         {
             get
             {
                 return BallsCheckDisabled();
             }
         }
 
         private bool BallsCheckDisabled()
         {
             bool check = balls.All(b => b.gameObject.activeSelf == false);
             if (check)
             {
                 return (true);
             }
             else
             {
                 return (false);
             }
         }


Since you are the only one who posted reply (and reply contains Linq) so i will accept yours. Cheers~

avatar image samvilm HajiyevEl · Feb 16 at 01:43 PM 0
Share

@HajiyevEl how do i use your script for my project. I have this but its not working. It says ".Any" is wrong : void start() { players = GameObject.FindWithTag("Player"); }

 public bool AllPlayersDisabled
          {
              get
              {
                  return PlayersCheckDisabled();
              }
          }
  
          private bool PlayersCheckDisabled()
          {
              bool check = players.Any(obj => obj.ActiveInHierarchy == false);
              if (check)
              {
                  return (true);
              }
              else
              {
                  return (false);
              }
          }
avatar image
0

Answer by sachinchetu · Jun 26, 2020 at 12:02 PM

You can use two list for this purpose to avoid costly iteration. You just need to take all active gameobjects in one list and whenever any gameobject inactive just remove it from the list and add in another list and vice-versa. By doing this you will can directly check if there is any element exists in inactive gameobjects list or not.

Or if you don't care about costly iteration, you can search element by listname.Contains(gameobject) and then check if this gameobject is active in hierarchy or not by using gameobject.activeSelf.

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

130 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Play Looped Sound? 4 Answers

Animation looping 0 Answers

My grid is getting created on the wonk 1 Answer

Running multiple coroutines in loops with different time 2 Answers

Is this loop infinite? 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