# for() { for() }

**URL:** https://discussions.unity.com/t/for-for/498264
**Category:** Scripting
**Tags:** Scripting
**Created:** [February 23, 2013, 4:18pm UTC](https://discussions.unity.com/t/for-for/498264 "2013-02-23T16:18:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![renman3000](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/renman3000/32/595476_2.png) [@renman3000](https://discussions.unity.com/u/renman3000)
#### Post date: [February 23, 2013, 4:18pm UTC](https://discussions.unity.com/t/for-for/498264/1 "2013-02-23T16:18:10Z")

</div>

Hi there,  
I have a problem with a “for() { for() { } }” statement.

It seems that once I run thru the second for () cycle, and reset back to the first for(), once I go back into the second for() the var i : int, is not reset, obviously, throwing off the second array range. So, how do I reset it?

```auto
function Start()
	for(var ca : int; ca < pathCamITween.length; ca++)
	{
		var pathCamData : iTweenPath;
		pathCamData = pathCamITween[ca].GetComponent(iTweenPath);
		
		for(var ci : int; ci < pathCamData.nodeCount; ci ++)
		{		
			pathCamNodeTemp = pathCamData.nodes[ci];
			setCamNode();	
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![chronos78](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@chronos78](https://discussions.unity.com/u/chronos78)
#### Post date: [February 23, 2013, 4:41pm UTC](https://discussions.unity.com/t/for-for/498264/2 "2013-02-23T16:41:14Z")

</div>

You are incrementing nodeCamCounter to a value of one before setting pathCam for the first time so element zero will never be set.

Try changing

```auto
function setCamNode()
{
    nodeCamCounter++;
    print("nodeCamCounter = " + nodeCamCounter);
    print("pathCam[] " + pathCam[nodeCamCounter]);
    pathCam[nodeCamCounter] = pathCamNodeTemp; 
}

```

```auto
function setCamNode()
{
    print("nodeCamCounter = " + nodeCamCounter);
    print("pathCam[] " + pathCam[nodeCamCounter]);
    pathCam[nodeCamCounter] = pathCamNodeTemp; 
    nodeCamCounter++;
}

```

---

<div class="post-metadata">

### Author: ![renman3000](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/renman3000/32/595476_2.png) [@renman3000](https://discussions.unity.com/u/renman3000)
#### Post date: [February 23, 2013, 5:11pm UTC](https://discussions.unity.com/t/for-for/498264/3 "2013-02-23T17:11:42Z")

</div>

Thanks you are right.

However, I am getting an index out of range at array[0] for this…

```auto
function Start()
{
	////retrieve all node positions from PAPath
	for(var i : int; i < pathBrainITween.length; i++)
	{
		var pathBrainData : iTweenPath;
		pathBrainData = pathBrainITween[i].GetComponent(iTweenPath);
		
		var pathBrainNodeCount : int = pathBrainData.nodeCount;
	
		for(var ib : int = 0; ib < pathBrainNodeCount; ib++)
		{					
			pathNodeTemp = pathBrainData.nodes[ib];
			setBrainNode();
			
			if(pathBrainNodeCount == ib)
			{
				ib = 0;
			}
		}
	}	
}
function setBrainNode()
{
	pathBrain[nodeCounter] = pathNodeTemp;	
	nodeCounter++;
}

```

@ Line 25, I am told the index is out of range when _nodeCounter == 0_. PathBrain:Vector3[&nbsp;] is set pre-hand correctly to accept the correct size. Completely unsure as I am using an identical procedure for a separate path which works.

??

---

<div class="post-metadata">

### Author: ![chronos78](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@chronos78](https://discussions.unity.com/u/chronos78)
#### Post date: [February 23, 2013, 5:25pm UTC](https://discussions.unity.com/t/for-for/498264/4 "2013-02-23T17:25:58Z")

</div>

Without the line number where the error is happening I can only guess which of the three arrays are causing it. If I had to guess I’d say you are hitting the error on line 35 with the pathBrain array since there is no error handling to ensure that nodeCounter is within the bounds of the array.

Also

```auto
if(pathBrainNodeCount == ib)
{
    print("pathBrainNodeRedux == ib. Reset ib");
    ib = 0;
}

```

is useless since the containing for loop will terminate at ib \< pathBrainNodeCount and if for some reason it ever did execute it would lock up your program since it would reset your counter thus putting it into an infinite loop.

Edit:  
Well seems I guessed correctly but you changed you original code so now line 35 is line 25 and since you are getting an index out of range with nodeCounter at zero that means you are probably missing the array initializer for pathBrain. So somewhere in your code before you get to this point there needs to be a pathBrain = new Array([sizeOfArray]). You should also ensure that nodeCounter \< pathBrain.Length before trying to set it.

---

<div class="post-metadata">

### Author: ![renman3000](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/renman3000/32/595476_2.png) [@renman3000](https://discussions.unity.com/u/renman3000)
#### Post date: [February 23, 2013, 7:31pm UTC](https://discussions.unity.com/t/for-for/498264/5 "2013-02-23T19:31:57Z")

</div>

Solved it.  
Thanks.

That code you mentioned is necessary since I want ib to be running. I do not want it to reset on the second entrance to the main for(). But thanks!

---

<div class="post-metadata">

### Author: ![chronos78](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@chronos78](https://discussions.unity.com/u/chronos78)
#### Post date: [February 23, 2013, 7:56pm UTC](https://discussions.unity.com/t/for-for/498264/6 "2013-02-23T19:56:15Z")

</div>

> [@renman3000](#):
>
> I do not want it to reset on the second entrance to the main for().

I’m not a Javascript guy but doesn’t the way you’ve defined your inner loop `for(var ib : int = 0; ib < pathBrainNodeCount; ib++)` mean that ib will automatically reinitialized to zero for every iteration of the outer loop and that the loop as defined will terminate at ib == pathBrainNodeCount - 1 so your if (ib == pathBrainNodeCount) will never evaluate as true?

---

<div class="post-metadata">

### Author: ![renman3000](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/renman3000/32/595476_2.png) [@renman3000](https://discussions.unity.com/u/renman3000)
#### Post date: [February 24, 2013, 1:39am UTC](https://discussions.unity.com/t/for-for/498264/7 "2013-02-24T01:39:28Z")

</div>

Yah maybe you re right now that I look at it. I must have placed that in there while I kept getting out of range index’s.
