- Home /
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?
How many of allProjectiles
have their render turned off?
In the beginning all of them have it off, but one by one each item gets its renderer turned on so eventually all of them will have it turned on. It's at this point that I simply want to add the first item in the list to the second list. Does that make sense?
I don't quite understand what you are trying to do, but maybe you are forgetting to clean the second list, or maybe you have more than one renderer turned off and that's why you are seeing two objects added.
Ok, I have several projectiles in the scene (I do not instantiate them, there is a finite number), all of which are referenced by the allProjectiles list on Start. By default all of these have their renderers turned off, and only when a projectile is needed (eg. a weapon is fired) I search the allProjectiles list for those with their renderers turned off, add them to a second list availableProjectiles (without removing them from the first), and then get the first item from availableProjectiles, turn on its renderer and fire it from the weapon.
So as you can see this will mean that eventually every projectile will have been fired i.e every projectile will have its renderer turned on. Therefore I need to be able to check for this condition and ins$$anonymous$$d of adding projectiles (from allProjectiles) with their renderer turned off, I will now add the first from this list ins$$anonymous$$d. The full script is available here in the first answer:
http://answers.unity3d.com/questions/451699/finding-the-first-gameobject-in-a-list-that-meets.html
Answer by Bunny83 · May 15, 2013 at 03:18 PM
This is simple boolean logic ;)
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
}
Yes, this looks like exactly what I was asking for. Please give me a $$anonymous$$ute while I test this (or ten :) ... ...
... hmmm, it seems to be working ok so far. I'll need to do more testing and tweak things a bit more to get everything working completely, but I think this was the missing piece to the puzzle. Thanks a lot for your help Bunny83
Answer by Aeron0 · May 15, 2013 at 02:50 PM
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++;
}
Some points:
First of all he's using UnityScript, not C#. That's not a problem, but it seems you misunderstood the problem.
He's using a list / array of GameObject. avProjectile is just the name of the for-each loop variable.
using a foreach loop and additionally keeping an index doesn't make much sense.
changing a collection while you iterate over it with foreach will throw an exception.
An answer should at least explain a little bit what you're actually doing.
Thanks for your help Aeron0, but unfortunately this didn't solve my problem. Appreciate your effort though
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Emptying a Generic List / Unexpected Behaviour 1 Answer
identify and remove an item from a list 1 Answer
how add values to Generic.list ? 2 Answers
Generic List.Count always gives 0 2 Answers