My scripting logic error, a solution?

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()”

so is this really stupid of me or there isnt a anwser to this?

I cannot really understand what are you trying to do. You say you want to “return a boolean”, but I can’t see any return statement.

Please explain a little more about your code if you want us to help you.

First of all :

if(objects[i]=true)

You need to replace that with

if(objects[i]==true)

to check for value. The equals isn’t used to evaluate, it’s used to assign. This change might fix the problem you’re facing.

Also, Unity does support the Contains method. Make sure you capitalize Contains, since it’s a method.

Hello, thanks for the reply’s but Diviner, your sugestion didn’t change anything. And unity editor says this about Contains:

So to i need to ad some extiensions before i can use Contains method with booleans?

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!

double post…