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;
}
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;
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?
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?