wrong neighbors List creation, fail at the OnTriggerEnter2D function
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.
Answer by Brocccoli · Mar 17, 2016 at 03:26 PM
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?
Hi, yeah missed this part,
created the test little project everything works fine,
going back to work on the main project, possible issue as you mentioned
the neighbor list of each called element.
Thanks mate.
Appreciate help.
No problem, if you could, just upvote the answer if you think it helped you.