- Home /
SplatMap error
I use some code to procedurally paint the terrain. But for some reason the splatmap variable sometimes becomes smaller or I atleast get a error with the current iteration being out of range. Error:
IndexOutOfRangeException: Array index is out of range.
(wrapper managed-to-managed) object:ElementAddr (object,int,int,int)
PT+$UpdateTile$72+$.MoveNext () (at Assets/Scripts/PT.js:272)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
$:MoveNext() (at Assets/Scripts/PT.js:98)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
StartMenu:SpawnPlayer() (at Assets/Scripts/StartMenu.js:716)
StartMenu:OnGUI() (at Assets/Scripts/StartMenu.js:273)
and for some reason when doing a check like the code below, it is the first line that returns the error, why doesn't that line prevent the error from occuring, since if it is out of range, it should be null right?
if (map[z,x,0] != null) { //For some reason it is this line that gives a null
map[z,x,0] = 1;
}
Answer by tanoshimi · Dec 09, 2014 at 04:02 PM
If your map is, say, a 2x2x2 array (i.e. map[2,2,2]) and you try to access an index outside that range (i.e. map[4,5,6]), you'll get an "array index is out of range" error as you have done.
Your if map[z,x,0] != null
check doesn't prevent this error - that tests whether a value at a given position in the array is null, but your array doesn't even contain that many elements.
Please paste the code where you declare map
, and where you assign the values z
and x
.
@tanoshimi Something like this:
var hw = Terrain.terrainData.heightmapWidth;
var map = new float[hw,hw,Terrain.terrainData.AlphamapLayers];
for (var z = 0; z < hw; z++) {
for (var x = 0; x < hw; x++) {
map[z,x,0] = 1;
}
}
The most weird part of this is that it is only sometimes that the error occurs. Even though the heightmapWidth never changes. So my guess would be that the AlphamapLayers is giving the error, I use this code to make the prototypes:
var splat : Texture2D[];
var detail : Texture2D[];
var tree : GameObject[];
private var splatPrototypes : SplatPrototype[];
private var detailProtoTypes : DetailPrototype[];
function CreateProtoTypes() {
splatPrototypes = new SplatPrototype[splat.Length];
for (var i = 0; i < splat.Length; i++) {
splatPrototypes[i] = new SplatPrototype();
splatPrototypes[i].texture = splat[i];
splatPrototypes[i].tileSize = new Vector2(15, 15);
}
detailProtoTypes = new DetailPrototype[detail.Length];
for (var d = 0; d < detail.Length; d++) {
detailProtoTypes[d] = new DetailPrototype();
detailProtoTypes[d].prototypeTexture = detail[d];
detailProtoTypes[d].render$$anonymous$$ode = DetailRender$$anonymous$$ode.GrassBillboard;
detailProtoTypes[d].maxHeight = 1;
detailProtoTypes[d].maxWidth = 1.5;
detailProtoTypes[d].dryColor = Grass;
detailProtoTypes[d].healthyColor = Grass;
}
}
Try:
var splatmapData = new float[Terrain.terrainData.alphamapWidth, Terrain.terrainData.alphamapHeight, Terrain.terrainData.alphamapLayers];
for(var z = 0; z < Terrain.terrainData.alphamapHeight; z++){
for(var x = 0; x < Terrain.terrainData.alphamapWidth; x++){