Index out of bounds

Hi!

I have no idea why I’m getting that the array index is out of range. I’ve tried a ton of different changes, but only THIS one carries out the intended purpose - but I get an IndexOutOfRangeException.

can anyone help me? I probably just need some fresh eyes.

	private Rect[] smallButtonPositions1;
	private Rect[] smallButtonPositions2;

void Start () {
		smallButtonPositions1 = new Rect[8];
		smallButtonPositions2 = new Rect[8];


		InitRects();
	}
void OnGUI()
	{

		
		for (int k = 0 ; k<smallButtonPositions1.Length;++k)
		{

				GUI.DrawTexture(smallButtonPositions1[4*k],smallRedButton);
				GUI.DrawTexture(smallButtonPositions1[4*k+1],smallGreenButton);
				GUI.DrawTexture(smallButtonPositions1[4*k+2],smallBlueButton);
				GUI.DrawTexture(smallButtonPositions1[4*k+3],smallYellowButton);
		
				GUI.DrawTexture(smallButtonPositions2[4*k],smallRedButton);
				GUI.DrawTexture(smallButtonPositions2[4*k+1],smallGreenButton);
				GUI.DrawTexture(smallButtonPositions2[4*k+2],smallBlueButton);
				GUI.DrawTexture(smallButtonPositions2[4*k+3],smallYellowButton);
				
				Debug.Log (4*k);
		}

	}

I define the rects later in my script, using an array of X/Y positions

I have similiar code running - and is working just perfect (like this is too), where I DON’T get the Index out of range exception?

This isn’t the entire script, as I’m fairly certain that the problem is within here

Just replace ++k with k++ :wink:
UPD: Sorry I was wrong. There is some thing strange with your code:

The lenght of your smallButtonPositions1 array is 8, and you go throgh this appay in the for cycle. But when your get k equals 2, in this line: GUI.DrawTexture(smallButtonPositions1[4k+1],smallGreenButton); you will try to get 4k+1 = 9’th element of array - and this throw an IndexOutOfRangeException.

        for (int k = 0 ; k<smallButtonPositions1.Length;++k)
        {
                GUI.DrawTexture(smallButtonPositions1[4*k],smallRedButton);
                GUI.DrawTexture(smallButtonPositions1[4*k+1],smallGreenButton); // this code, and code bellow will work only while k less then 2
                GUI.DrawTexture(smallButtonPositions1[4*k+2],smallBlueButton);
                GUI.DrawTexture(smallButtonPositions1[4*k+3],smallYellowButton);
...

Yeah, that didn’t do the trick! I don’t get it, as I have this right above and it works ( in OnGUI)

for (int k = 0 ; k<rowAlterations.Length;++k)
		{
			if(rowAlterations[k] == false)
			{
				GUI.DrawTexture(largeButtonPositions[2*k],greenButtonTexture);
				GUI.DrawTexture(largeButtonPositions[2*k+1],redButtonTexture);
			}
			if(rowAlterations[k] == true)
			{
				GUI.DrawTexture(largeButtonPositions[2*k],redButtonTexture);
				GUI.DrawTexture(largeButtonPositions[2*k+1],greenButtonTexture);
			}

I fixed it by just stacking a pile of

GUI.DrawTexture(smallButtonPositions1[0],smallRedButton);
GUI.DrawTexture(smallButtonPositions1[1],smallGreenButton);
GUI.DrawTexture(smallButtonPositions1[2],smallBlueButton);
GUI.DrawTexture(smallButtonPositions1[3],smallYellowButton);
GUI.DrawTexture(smallButtonPositions1[4],smallRedButton);
GUI.DrawTexture(smallButtonPositions1[5],smallGreenButton);
GUI.DrawTexture(smallButtonPositions1[6],smallBlueButton);
GUI.DrawTexture(smallButtonPositions1[7],smallYellowButton);

GUI.DrawTexture(smallButtonPositions2[0],smallRedButton);
GUI.DrawTexture(smallButtonPositions2[1],smallGreenButton);
GUI.DrawTexture(smallButtonPositions2[2],smallBlueButton);
GUI.DrawTexture(smallButtonPositions2[3],smallYellowButton);
GUI.DrawTexture(smallButtonPositions2[4],smallRedButton);
GUI.DrawTexture(smallButtonPositions2[5],smallGreenButton);
GUI.DrawTexture(smallButtonPositions2[6],smallBlueButton);
GUI.DrawTexture(smallButtonPositions2[7],smallYellowButton);

but this obviously isn’t the most optimal fix.
I’m still really curious to find the cause of the problem

Yeah, I’ve understood what you need!
Try % operator (Arithmetic operators - C# reference | Microsoft Learn)

See snippet bellow:

...
	Texture[] smallButtons = new Texture[]{ smallRedButton, smallGreenButton, smallBlueButton, smallYellowButton };

	for(int i = 0; i < smallButtonPositions1.Lenght; i++)
	{
		GUI.DrawTexture(smallButtonPositions1[i], smallButtons[i % 4]);
		GUI.DrawTexture(smallButtonPositions2[i], smallButtons[i % 4]);
	}
...