- Home /
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>();
}
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)
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~
@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);
}
}
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.
Your answer

Follow this Question
Related Questions
Is there a way to call up a IF statement like a function? 3 Answers
How to get all properties from a component at once? 1 Answer
Animation Looping Help 3 Answers
For loop going based off of Time.deltatime? 1 Answer
gameobject list 2 Answers