提问人:Darren 提问时间:3/27/2022 最后编辑:Darren 更新时间:3/28/2022 访问量:298
尚未初始化为特定大小的数组上的越界异常
Out of bounds exception on array that has not been initialized to a specific size
问:
C# Unity 编码
我设置了几个这样的数组。
public float[] arrayName;
我的代码中没有固定数量的元素
我最多可以访问 6 个数组,没有问题,但是当我到达 7 个时,我得到了一个越界异常。
在我的代码中没有任何地方将它们初始化为特定数量,即使我尝试
public float[] arrayName = new float[8]
例如,然后运行调试日志以获取长度
我仍然得到 7,但无法访问超过 6 分。我远非专家。
事情是这样的......
我还有几个纹理 2d 数组......
public Texture2D[] arrayName;
我遇到了同样的问题,但是当我这样设置它们时
public Texture2D[] arrayName = new Texture2D[8];
一切都很好。
我很困惑......
我也试过这个。
public float[] array name = new float[] {1f, 2f, 3f, 4f ,5f, 6f, 7f, 8f}
等相同的结果
不知何故,我的数组被限制为 7 个。当我问这个问题时,我将尝试创建新的数组,这些数组将像这样初始化
public float[] newArrayName = new float[8];
看看会发生什么。但我很想知道我显然做错了什么或不理解什么。谢谢
这些是我的数组。在我尝试访问 7 之前,一切都很完美。然后出界错误。
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;
我用于将控制设置分配给数组的代码。所有人都按预期执行 0 到 6
#region Texture One 折叠 terrain.showTextureOne = EditorGUILayout.Foldout(terrain.showTextureOne, “纹理一”, EditorStyles.boldLabel); 如果 (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();
}
例如,当我复制粘贴工作代码并简单地将 6 修改为 7 时,这就是现在和一直需要的,混乱!:)示例:请注意,修改了 Texture2D 数组,并且没有错误地工作。数组声明完全相同,但正如你所看到的,我在 normalScale 行中将一个 6 更改为 7,除了越界错误外,什么都没有。在其他代码中,我忘记了更改其他内容,这导致了这种情况,但我煞费苦心地确保这不是问题所在。
#region 纹理 Eight 折叠 terrain.showTextureEight = EditorGUILayout.Foldout(terrain.showTextureEight, “纹理八”, 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);
答: 暂无答案
评论