0

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 has 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 ... ?

Darren
  • 3
  • 4
  • There is no way, you can access elements of an array which hasn't been initialized. So there is something going on you are not showing here – derpirscher Mar 27 '22 at 13:30
  • 1
    Can you post a [mcve]? This will allow other users to reproduce your problem locally and offer better advice. – BJ Myers Mar 27 '22 at 15:15
  • Thanks guys. I am working but when I get home I will try to post an example. Mostly everything is fine up to a point so the arrays work as expected. I am coding in unity. I think it might be a unity bug. Gonna reboot that when I get home first to see if it helps. Really makes no sense. I initialized one to 90 which is far more than needed. Same issue. Should definitely not be out of bounds. I am no pro but I do know how to do arrays. I think something else causing it but haven't been able to track it down. – Darren Mar 27 '22 at 16:41
  • Unity is special, the array size is set from the UI when you are inspecting the component it is attached to – Scott Chamberlain Mar 27 '22 at 18:13
  • I edited my post. I hope I did it right. IF not if you can help I can try and fix it but makes the point I hope. – Darren Mar 27 '22 at 18:20
  • @ScottChamberlain Can you possibly explain more of what you are saying. I have been thinking similar as it being unity but I am no expert. I haven't touched code or unity for a few years so kind of a rookie again. – Darren Mar 27 '22 at 18:27
  • OK, figured out what I was doing wrong or what I forgot to do. In OnEnable() I had to normalScale = new float[8]; metallic = new float[8]; duh! Been a long while. Still not sure why all others worked without being initialized but this has fixed my problem. Rookie mistake. Thanks guys. They have to be initialized in either awake or OnEnable() probably could do it in start() also but I am executing in edit mode and OnEnable() best for that. – Darren Mar 27 '22 at 18:41
  • @derpirscher you are kinda right. I hadn't initialized them but for some reason they were still working perfectly. Once I initialized problem solved. Unsure why it worked up till 7 but not looking a gift horse in the mouth. Weird. Thanks. You guys all made me think – Darren Mar 27 '22 at 19:32
  • To anyone coming across this. Just Declare and initialize the variables at once. Forget OnEnable() etc. Like this. public class YourClassName : MonoBehaviour { public float[] normalScale = new float [7]; – Darren Mar 28 '22 at 11:57

0 Answers0