Array index is out of range???

Hi Guys,

Bit baffled right now :S Wondering why my array is out of range, if someone could help me out I would appreciate it…

This is a drawing editor thing, creates a bunch of boxes which the user can draw a design onto, the boxes are meant to change colour but due to this error they won’t do anything I want them to…

void DoCanvas () {
		
		pixelTexture = (Texture2D) EditorGUILayout.ObjectField("Image", pixelTexture, typeof (Texture2D), false);

		
		dHeight = EditorGUILayout.Slider ("Drawing Width", dHeight, 0, 50);
		dWidth = EditorGUILayout.Slider ("Drawing Height", dWidth, 0, 50);
		
		Event evt = Event.current;
		
		var oldColor = GUI.color;
		
		//GUILayout.BeginHorizontal();
		EditorGUILayout.BeginHorizontal();
		for (int x = 0; x < dHeight; x++){
			
			EditorGUILayout.BeginVertical();
        	for (int z = 0; z < dWidth; z++){
				
				//var index = x + z * height;
				int index = x + z * height;
				
				//GUI.color = pixels[index];
				
				var pixelRect = GUILayoutUtility.GetRect(GUIContent.none, "Box", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
				GUI.DrawTexture(pixelRect, pixelTexture);

				if((evt.type == EventType.MouseDown)  pixelRect.Contains(evt.mousePosition)) {
					if(evt.button == 0){
						pixels[index] = selectedColor;
						Debug.Log("Clicked Ting");
					}else {
						pixels[index] = eraseColor;
						Debug.Log("Erased Ting");
					}
					evt.Use();
				}
				
				//GUILayout.Box(GUIContent.none, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
				//GUILayout.Box(GUIContent.none, GUILayout.Width(30), GUILayout.Height(30));
            }
			EditorGUILayout.EndVertical();
		}
		EditorGUILayout.EndHorizontal();
		
		GUI.color = oldColor;
	}

Thanks,

Scott.

Oh I forgot to say the line of code unity is notifying me about is this one:

pixels[index] = selectedC olor;

and this one: GUI.color = pixels[index];

basically when ever I am trying to use my pixels variable which is a Color[ ] var…

Thanks.

Where do you initialize your pixels array? I don’t see it here.

Hi Thanks for your quick response!

I initiate it here:

void OnEnabled () {
		width = height = 8;
		pixels = new Color[width * height];
		
		for(int i = 0; i < pixels.Length; i++){
			pixels[i] = randomColor();	
		}
	
		pixelTexture = EditorGUIUtility.whiteTexture;
		selectedColor = Color.white;
	}

Make sure your sliders values start with 0 arrays are 0 indexed which means they start from 0
So if you have an array that has 8 items in the array your index would be from 0-7 not 1-8

So if your slider returns something higher than 7 it will give you the error you are receiving.

so make sure that your following variable will always be within the range of your array.
int index = x + z * height;

Hi, Thanks for a response! Really appreciate it!!

I changed : width = height = 8 to:

width = height = 7 so therefore less than 8 as you said, but I still get the error. any reasons as to why?

Thanks,

Scott.

Are dHeight and dWidth integers? If they’re not integers, then 7 < 7.5 and you’ll go out of bounds when you’re using the slider, which doesn’t return integers.

You also need to set pixels to the maximum amount that height and width can be. It looks like you can slide from zero to fifty, but you’re only allocating space for 8x8?

dHeight and dWidth are integers yes. So thats not the problem is it?

Thanks,

Scott.

Either dwidth or dheight is greater than 7, or less than 0. That’s the ONLY way you would get this error. Run in the debugger, and when you get the error, look at the values of those two variables.

Why are you using a single dimension array?

pixels = new Color[width * height];

Is it appropriate in your code to use a multidimensional array?

Color[,] pixels = new Color[width, height];