Paint Texture Tools error index out of range error.

[Unity Version]: 2021.3.8f1 LTS

Hey, i was painting terrain in unity, and something happened, i tried making a sixth terrain texture layer, but it for some reason was acting really strange, unity wouldnt let me click it at all, and thus, i cant paint with more than five layers, which is very detrimental.

It also started showing me an error, it says

IndexOutOfRangeException: Index was outside the bounds of the array.
UnityEditorInternal.ReorderableList.GetElementHeight (System.Int32 index) (at <5f40cdb07bd44d76a23dad985a4ec283>:0)
UnityEditorInternal.ReorderableList.GetClampedDragPosition (UnityEngine.Rect listRect) (at <5f40cdb07bd44d76a23dad985a4ec283>:0)
UnityEditorInternal.ReorderableList.CalculateRowIndex (UnityEngine.Rect listRect) (at <5f40cdb07bd44d76a23dad985a4ec283>:0)
UnityEditorInternal.ReorderableList.DoDraggingAndSelection (UnityEngine.Rect listRect) (at <5f40cdb07bd44d76a23dad985a4ec283>:0)
UnityEditorInternal.ReorderableList.DoListElements (UnityEngine.Rect listRect, UnityEngine.Rect visibleRect) (at <5f40cdb07bd44d76a23dad985a4ec283>:0)
UnityEditorInternal.ReorderableList.DoLayoutList () (at <5f40cdb07bd44d76a23dad985a4ec283>:0)
UnityEditor.TerrainTools.PaintTextureTool.LayersGUI (UnityEngine.Terrain terrain, UnityEditor.TerrainTools.IOnInspectorGUI editContext) (at Library/PackageCache/com.unity.terrain-tools@4.0.3/Editor/TerrainTools/PaintTextureTool.cs:458)
UnityEditor.TerrainTools.PaintTextureTool.OnInspectorGUI (UnityEngine.Terrain terrain, UnityEditor.TerrainTools.IOnInspectorGUI editContext) (at Library/PackageCache/com.unity.terrain-tools@4.0.3/Editor/TerrainTools/PaintTextureTool.cs:341)
UnityEditor.TerrainInspector.ShowPaint () (at <5f40cdb07bd44d76a23dad985a4ec283>:0)
UnityEditor.TerrainInspector.OnInspectorGUI () (at <5f40cdb07bd44d76a23dad985a4ec283>:0)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <77cfaa957c26445e8d2fa87bf3ff3fa6>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

How would i go about fixing this?

1 Like

Maybe a little late to the party, but had the same problem and figured it out if someone needs this in the future.
The bug occurs when one of the terrain layers becomes null.
An easy fix for this is to remove or comment out null checks in UpdateLayerPalette() and LoadPalette().

Basically from line 761 to 770 it should look like:

//if (layer != null)
//{
    Layer paletteLayer = new Layer();
    paletteLayer.AssignedLayer = layer;
    paletteLayer.IsSelected = selectedList.ElementAtOrDefault(index);
    m_PaletteLayers.Add(paletteLayer);
    if (layer == m_SelectedTerrainLayer)
        m_LayerList.index = index;
    index++;
//}

And from line 805 to 811:

//if (layer != null)
//{
    Layer newLayer = new Layer();
    newLayer.AssignedLayer = layer;
    m_PaletteLayers.Add(newLayer);
    terrainLayers.Add(layer);
//}

Hope it helps someone!