2022-03-28

Out of bounds exception on array that has not been initialized to a specific size

C# Coding for unity

I have several arrays set as such.

public float[] arrayName;

no set number of elements anywhere in my code

I can access the arrays up to 6 no problems but when I get to 7 I get an out of bounds exception.

No where in my code does it initialize them to a specific amount and even when I try

public float[] arrayName = new float[8] 

for instance then run a debug log to get the length

I still get 7 and cannot access past 6. I am far from an expert.

Here is the thing...

I also have a couple texture 2d arrays...

public Texture2D[] arrayName; 

which I was having the same problem with but when I set them like this

public Texture2D[] arrayName = new Texture2D[8];

all is fine.

I am so confused...

I have also tried this.

public float[] array name = new float[] {1f, 2f, 3f, 4f ,5f, 6f, 7f, 8f}

etc. same result

Somehow my arrays are being limited to 7. As I am asking this I am going to try creating new arrays which will be initialized like such

public float[] newArrayName = new float[8];

and see what happens. But I would love to learn what I am obviously doing wrong or not understanding. Thanks

These are my arrays. All work perfect up to when I try to access 7. Then Get out of bounds errors.

 public Texture2D[] terrainTexture;
public Texture2D[] terrainTextureNormal;
public float[] normalScale;
public Color[] specular;
public float[] metallic;
public float[] smoothness;
public float[] maxHeight;
public float[] minHeight;
public float[] maxSteepness;
public float[] minSteepness;
public float[] weight;
public float[] blend;

The code I use to assign the control settings to the arrays. All do as expected 0 to 6

#region Texture One Foldout terrain.showTextureOne = EditorGUILayout.Foldout(terrain.showTextureOne, " Texture One", EditorStyles.boldLabel); if (terrain.showTextureOne) {

        GUILayout.Space(20);
        terrain.terrainTexture[0] = (Texture2D)EditorGUILayout.ObjectField("Diffuse", terrain.terrainTexture[0], typeof(Texture2D), true);
        GUILayout.Space(2);
        terrain.terrainTextureNormal[0] = (Texture2D)EditorGUILayout.ObjectField("Normal Map", terrain.terrainTextureNormal[0], typeof(Texture2D), true);
        GUILayout.Space(6);
        terrain.normalScale[0] = EditorGUILayout.Slider("Normal Scale", terrain.normalScale[0], 0f, 1f);
        GUILayout.Space(10);
        terrain.metallic[0] = EditorGUILayout.Slider("Metallic",terrain.metallic[0], 0f, 1f);
        GUILayout.Space(10);
        terrain.smoothness[0] = EditorGUILayout.Slider("Smoothness", terrain.smoothness[0], 0f, 1f);
        GUILayout.Space(10);
        terrain.maxHeight[0] = EditorGUILayout.Slider("Max Height", terrain.maxHeight[0], 0f, terrain.terrainHeight);
        GUILayout.Space(10);
        terrain.minHeight[0] = EditorGUILayout.Slider("Min Height", terrain.minHeight[0], 0f, terrain.terrainHeight);
        GUILayout.Space(10);
        terrain.maxSteepness[0] = EditorGUILayout.Slider("Max Steepness",terrain.maxSteepness[0], 0, 90);
        GUILayout.Space(10);
        terrain.minSteepness[0] = EditorGUILayout.Slider("Min Steepness", terrain.minSteepness[0], 0, 90);
        GUILayout.Space(10);
        terrain.weight[0] = EditorGUILayout.Slider("Weight", terrain.weight[0], 0f, 1f);
        GUILayout.Space(10);
       
        terrain.size[0] = EditorGUILayout.Vector2Field("Size", terrain.size[0]);
        terrain.offset[0] = EditorGUILayout.Vector2Field("Offset", terrain.offset[0]);
        GUILayout.Space(10);
        GUILayout.BeginHorizontal();

        if (GUILayout.Button("Set Properties"))
        {
            terrain.ModifyTextures();
        }
        if (GUILayout.Button("Save Properties"))
        {
            terrain.SaveEditorSettings();
        }
        GUILayout.EndHorizontal();


    }

When I copy paste working code and simply modify 6 to 7 for example which is all that is and as been needed, chaos! :) Example notice the Texture2D Arrays modified and work with no errors. Arrays declared exactly the same but as you can see I change one 6 to 7 as in the normalScale line, nothing but out of bounds errors. There have been other things I have forgotten to change in other code which has caused this but I have painstakingly made sure that is not the problem.

#region Texture Eight Foldout terrain.showTextureEight = EditorGUILayout.Foldout(terrain.showTextureEight, " Texture Eight", EditorStyles.boldLabel);

    if (terrain.showTextureEight)
    {
        GUILayout.Space(20);
        terrain.terrainTexture[7] = (Texture2D)EditorGUILayout.ObjectField("Diffuse", terrain.terrainTexture[7], typeof(Texture2D), true);
        GUILayout.Space(2);
        terrain.terrainTextureNormal[7] = (Texture2D)EditorGUILayout.ObjectField("Normal Map", terrain.terrainTextureNormal[7], typeof(Texture2D), true);
        GUILayout.Space(6);
        
        terrain.normalScale[7] = EditorGUILayout.Slider("Normal Scale", terrain.normalScale[7], 0f, 1f);
        GUILayout.Space(6);
        terrain.metallic[6] = EditorGUILayout.Slider("Metallic", terrain.metallic[6], 0f, 1f);
        GUILayout.Space(10);
        terrain.smoothness[6] = EditorGUILayout.Slider("Smoothness", terrain.smoothness[6], 0f, 1f);

This is my custom editor. One through 7 work as planned. Click 8 and ... ?



No comments:

Post a Comment