Question by
PinkPumpkin · Aug 19, 2016 at 01:42 PM ·
c#textureterraindataarray-out-of-range-except
[C#] Error in script [IndexOutOfRangeException]
Hello,
Unity/ C# newbie here.
I found following code in this unity forums thread, all credits to @LamentConfig
Unfortunately, in contrast to others, I am not able to get it running. Following Error message occurs in the Unity Console:
IndexOutOfRangeException: Array index is out of range.
(wrapper managed-to-managed) object: ElementAddr_3_4 (object, int, int, int)
Code: (I added a public Textures[] array and dragged my textures in there)
using UnityEngine;
using System.Collections;
public class terrainmod : MonoBehaviour
{
public Texture2D [] alphamaptextures;
void Start ()
{
TerrainData terrainData = Terrain.activeTerrain.terrainData; // grab the active terrain
float[,,] splatmapData = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, terrainData.alphamapLayers]; // create array
// for every x and y location within terrain alter the texture :
for (int y = 0; y < terrainData.alphamapHeight; y++)
{
for (int x = 0; x < terrainData.alphamapWidth; x++)
{
splatmapData[x, y, 0] = 0;
splatmapData[x, y, 1] = 0;
splatmapData[x, y, 2] = 0;
splatmapData[x, y, 3] = 1; // set the entire terrain to the fourth texture
splatmapData[x, y, 4] = 0;
}
}
terrainData.SetAlphamaps(0, 0, splatmapData);
}
void Update ()
{
}
}
Thanks for reading, any help is highly appreciated!
Comment
Answer by PinkPumpkin · Aug 19, 2016 at 07:47 PM
I figured it out myself, the textures must be dragged into the terrain in editor mode. In my case I dragged not enough textures in there.