loop break command not working

I have a loop that finds objects next to a centre object, adds them to an array, and searches for their neighbours until it has found all the objects that neighbour each other. Unfortunately, when I get to the end of the loop,I get an array out of range index, why is the break command not working?

Here are the first 2 lines, how do I break ?

       for ( var w2:int=0; w2 < 400; w2 ++) {


      centreobject = foundwalls[w2];
     --==find objects around centreobject and add them to foundwalls[];==--

   	//if ( w2 == foundwalls.length-1 ){break; }// <---tried this! still goes out of range!
            }

The break command always works, so logically that means the if statement isn’t executing, or possibly that it’s inside another loop (since break only breaks out of one level). You should learn basic debugging techniques, such as using the Debug.Log command to track what your code is doing. (Although I’m not sure why you wouldn’t just use w2 < foundwalls.length in the for loop anyway, rather than an arbitrary 400.)