How can I check if ALL items in an array/list meet a condition?

I have a generic list (but this can apply to arrays also) which I check to see if any of the items have their renderer turned off, those that do simply get added to another list. I want to add in a condition in case none (not some) of the items have their renderers turned off i.e all of the items have their renderers turned on. I’m not sure how to do this. I’ve tried using an else statement, which when met, I would like to add only the first item in the current list to another list; but for some reason it always adds 2 of this item instead of one. Should I make a separate for loop just for this, or what should I do?

This is simple boolean logic :wink:

All you need is a boolean value that is initially true. Inside your loop you set this boolean to false whenever one of the elements does not meet the condition. If this variable is still true after the loop is done, all elements met the condition.

In your case something like that:

var allReady = true;
for (var avProjectile : GameObject in allProjectiles)
{
    if (avProjectile.renderer.enabled)
    {
        allReady = false;
    }
}
if (allReady)
{
    // All renderers are off
}

using System.Collections.Generic;

// Generic List array variables to hold your data
List<avProjectile> allProjectiles;
List<avProjectile> noRender;

// These variables need instantiated
allProjectiles = new List<avProjectile>();
noRender = new List<avProjectile>();

// Add your data in the list
allProjectiles.Add(your avProjectiles);


if (Input.GetKeyDown ("space")) 
{
  int index = 0;
  
  foreach(avProjectile projectile in allProjectiles)
  {
    if(allProjectiles[index].renderer.enabled == false)
    {
      // Add the index projectile in the new list 
      noRender.Add(allProjectiles[index]);

      // remove that projectile from the old list
      allProjectiles.RemoveAt(index);
     }
     index++;
   }