c# foreach does not work

Hello,

please help me, I’m desperate and cannot find the solution.

I’ve asked the same question in the general questions and then learned that
one might be able to get help much sooner via the help room.

Below is the code that does not work:

List<sq> aList = new List<sq> ();
compileList(sqA, ref aList);

public void compileList(sq e, ref List<sq> iList){
	foreach(sq s in e.neighbors){
		compileList(s, ref iList);
	}
}

sq class contains

public class sq : MonoBehaviour {
	public List<sq> neighbors = new List<sq> ();
	...
}

but the foreach loop is not even called to every recursive neighbor : (

Neighbors are constructed like this:

sqA - sqB - sqC - sqD - sqE

where, for example sqB has neighbors sqA and sqC,

and sqA has a single neighbor sqB.

Any help much appreciated.

The logic in compileList looks sound for the most part. Though you are never adding anything to the list. You’re just calling compileList a bunch of times.

iList.Add(e)
foreach(sq s in e.neighbors){
   compileList(s, ref iList);
 }

That may get you what you’re looking for.

Also, try debugging. Does the initial call to e.neighbors have something in it, or is it empty?