Well basically I want to return a boolean from an array with a foreach loop. My problem is that my code only returns true whe the last boolean is true.
// Create a boolean array with the matching length of my objects
bool[] objects = new bool[myobject.Length];
// Foreach loop with lambdas
for (int i=0; i< myobject.Length; i++)
{
// Get A specific object
MYOBJECT o = myobject[i];
// Return something for a specific object and blace it to the objects boolean array
if (o.GetSomething())
{
objects[i] = true;
}
else
{
objects[i] = false;
}
// For each object that returns true in objects array we apply an action
if(objects[i]=true)
{
// Get more specific with a string...
if (o.GetTag()=="yes")
{
DoSomething();
}
}
}
…so yeah im running out of ideas, one idea was to use the final command out of the loop, with a “objects.contains(true)” command but unity seems to not suport “.contains()”
yes, you would need to be using Linq to use contains, but faster would be to define a bool variable outside the loop that is set to false, and set to true in the loop as soon as you get a true value. Then after the loop, if the var is true, at least 1 element is true and if it is false, everything is false.
Well id not like to use Linq in my project, because i’m going to use this just for once and i think there is no point to add Linq for just one silly method. Anyway I’ll Try your idea in a moment and see what turns out…
Woulnd’t this just be easyer? Unless im missing someting.
// Foreach loop with lambdas
for (int i=0; i< myobject.Length; i++)
{
// Get A specific object
MYOBJECT o = myobject[i];
if (o.GetSomething() o.GetTag()=="yes")
{
DoSomething();
}
}
I guess you could also
// Create a boolean array with the matching length of my objects
bool[] objects = new bool[myobject.Length];
// Foreach loop with lambdas
for (int i=0; i< myobject.Length; i++)
{
// Get A specific object
MYOBJECT o = myobject[i];
// Return something for a specific object and blace it to the objects boolean array
if (o.GetSomething())
{
objects[i] = true;
}
else
{
objects[i] = false;
}
}
// For each object that returns true in objects array we apply an action
for (int i=0; i< myobject.Length; i++)
{
if(objects[i]=true myobject[i].GetTag()=="yes")
{
DoSomething();
}
}
Hey eArtist, thats funny I didnt try the first method, altho its so simple yet it works excactly how i want it to, i wonder what went wrong at the first place…
Anyway thank you alot!