- Home /
Array index is out of range
Hi, I have these code, but always get "Array index is out of range" error.
var vertices : Vector3[] = new Vector3[10];
function Update () { if(Input.GetKeyDown(KeyCode.I)){ vertices[0] = new Vector3( 1, 1, 1); } }
can someone help me to solve this problem?? thanks.
gear$$anonymous$$axValue = gearRatio[i];
This is the line that shows array index out of range. This is the section of code where it is
//Engine Sound
function EngineSound(){
for (var i = 0; i < gearRatio.length; i++){
if(gearRatio[i]> currentSpeed){
break;
}
}
var gear$$anonymous$$inValue : float = 0.00;
var gear$$anonymous$$axValue : float = 0.00;
if (i == 0){
gear$$anonymous$$inValue = 0;
}
else {
gear$$anonymous$$inValue = gearRatio[i-1];
}
gear$$anonymous$$axValue = gearRatio[i];
var engine$$anonymous$$ch : float = ((currentSpeed - gear$$anonymous$$inValue)/(gear$$anonymous$$axValue - gear$$anonymous$$inValue))+1;
GetComponent.<AudioSource>().pitch = engine$$anonymous$$ch;
}
Answer by Mike 3 · Jul 22, 2010 at 02:31 PM
Make the array private
Unity will use the inspector version of the array for any public arrays, and since you probably didn't fill values in there, it'll assign a 0 length array
private var vertices : Vector3[] = new Vector3[10];
Answer by PatHightree · Jul 22, 2010 at 11:13 PM
Don't initialize class variables at declaration time, doing it in Awake() or Startup() is the proper way to do it.
Answer by sikha · Feb 12, 2012 at 01:19 PM
I have same problem when use followin code on Player
var healthTex: Texture2D[]; // define the size and fill its elements in the Inspector
static var LIVES = 6;
var healthImage: Texture2D;
var healthImageOffset = Vector2(0, 0);
var nativeVerticalResolution = 1200.0;
function Update(){
guiTexture.texture = healthTex[LIVES];
DrawImageBottomAligned( healthImageOffset, healthImage);
}
function DrawImageBottomAligned (pos : Vector2, image : Texture2D)
{
GUI.Label(Rect (pos.x, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image);
}
Your answer
Follow this Question
Related Questions
IndexOutOfRange Issue 1 Answer
IndexOutOfRangeException C# 1 Answer
Array index is out of range 0 Answers
What means this error ? 1 Answer
IndexOutOfRangeException: Array index is out of range. 1 Answer