Question by
williamreinsch · Jan 11, 2021 at 02:13 PM ·
errorarrayindexindexoutofrangeexceptionarray-out-of-range-except
IndexOutOfRangeException: Index was outside the bounds of the array. (wrapper managed-to-managed) System.Object.ElementAddr_4(object,int,int,int)
Hey I am super stuck with my unity game trying to add sound to my characters footsteps :(
https://www.youtube.com/watch?v=Bnm8mzxnwP8 I was following this tutorial and got to the end then when i press play in unity and walk it crashes and gives me this error -
IndexOutOfRangeException: Index was outside the bounds of the array.
(wrapper managed-to-managed) System.Object.ElementAddr_4(object,int,int,int)
TerrainDetector.GetActiveTerrainTextureIdx (UnityEngine.Vector3 position) (at
Assets/TerrainDetector.cs:39)
FootSteps.GetRandomClip () (at Assets/FootSteps.cs:29)
FootSteps.Step () (at Assets/FootSteps.cs:23)
Here is the code for TerrainDetector:
using UnityEngine;
public class TerrainDetector
{
private TerrainData terrainData;
private int alphamapWidth;
private int alphamapHeight;
private float[,,] splatmapData;
private int numTextures;
public TerrainDetector()
{
terrainData = Terrain.activeTerrain.terrainData;
alphamapWidth = terrainData.alphamapWidth;
alphamapHeight = terrainData.alphamapHeight;
splatmapData = terrainData.GetAlphamaps(0, 0, alphamapWidth, alphamapHeight);
numTextures = splatmapData.Length / (alphamapWidth * alphamapHeight);
}
private Vector3 ConvertToSplatMapCoordinate(Vector3 worldPosition)
{
Vector3 splatPosition = new Vector3();
Terrain ter = Terrain.activeTerrain;
Vector3 terPosition = ter.transform.position;
splatPosition.x = ((worldPosition.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth;
splatPosition.z = ((worldPosition.z - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight;
return splatPosition;
}
public int GetActiveTerrainTextureIdx(Vector3 position)
{
Vector3 terrainCord = ConvertToSplatMapCoordinate(position);
int activeTerrainIndex = 0;
float largestOpacity = 0f;
for (int i = 0; i < numTextures; i++)
{
if (largestOpacity < splatmapData[(int)terrainCord.z, (int)terrainCord.x, i])
{
activeTerrainIndex = i;
largestOpacity = splatmapData[(int)terrainCord.z, (int)terrainCord.x, i];
}
}
return activeTerrainIndex;
}
}
and here is the code for FootSteps: using UnityEngine;
public class FootSteps : MonoBehaviour
{
[SerializeField]
private AudioClip[] stoneClips;
[SerializeField]
private AudioClip[] mudClips;
[SerializeField]
private AudioClip[] grassClips;
private AudioSource audioSource;
private TerrainDetector terrainDetector;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
terrainDetector = new TerrainDetector();
}
private void Step()
{
AudioClip clip = GetRandomClip();
audioSource.PlayOneShot(clip);
}
private AudioClip GetRandomClip()
{
int terrainTextureIndex = terrainDetector.GetActiveTerrainTextureIdx(transform.position);
switch(terrainTextureIndex)
{
case 0:
return stoneClips[UnityEngine.Random.Range(0, stoneClips.Length)];
case 1:
return mudClips[UnityEngine.Random.Range(0, mudClips.Length)];
case 2:
default:
return grassClips[UnityEngine.Random.Range(0, grassClips.Length)];
}
}
}
Hope you can help! It would mean a lot :D
Comment
Your answer
Follow this Question
Related Questions
,"For" loop looping only once through my array 0 Answers
Array index is out of range (C#)? 1 Answer
Unity C# array index is out of range 1 Answer
Unity Inspector public large array - Lag and errors - Optimized GUI Block text buffer too large 0 Answers
Getting an error when using an Array of String Arrays 0 Answers