Fazle Rabbi
Fazle Rabbi

Reputation: 340

How to solve IndexOutOfRangeException?

I have some error related to IndexOutOfRangeException. The number of errors is over 200 and the error comes up when I play the game.

Error shown like this:

IndexOutOfRangeException: Array index is out of range. GameController.OnGUI () (at Assets/MicroRacers Assets/Scripts/GameController.js:67)

Here is the code of my game controller.

var CheckPoints:Transform[];

var LapsToWin:int = 5;
var PointsPerPlace:Array = new Array(8,4,2,1);

var CountDownToStart:float = 5;

var Items:Transform[];
var ItemCopy:Object;
var SpawnTime:Vector2 = Vector2(5,10);
private var SpawnTimeCount:float = 0;
var MaximumItems:int = 5;
private var ItemCount:int = 0;

private var FinishPlace:int = 0;

private var Players:int = 1;
var RaceEndDelay:float = 5;

//~ function Awake()
//~ {
    //~ Application.targetFrameRate = 30;
//~ }

function Start()
{
    SpawnTimeCount = Random.Range(SpawnTime.x, SpawnTime.y);
}

function Update () 
{
    if ( SpawnTimeCount > 0 )
    {
        SpawnTimeCount -= Time.deltaTime;
    }
    else if ( ItemCount < MaximumItems )
    {
        SpawnTimeCount = Random.Range(SpawnTime.x, SpawnTime.y);
        ItemCount++;

        ItemCopy = Instantiate(Items[Mathf.Floor(Random.Range(0, Items.length - 1))], CheckPoints[Mathf.Floor(Random.Range(0, CheckPoints.length - 1))].position + Vector3.up * 0.3, Quaternion.identity);
        ItemCopy.transform.Translate(Vector3.right * Random.Range(-3,3), Space.Self);
    }

    if ( Players == 0 )
    {
        RaceEndDelay -= Time.deltaTime;

        if ( RaceEndDelay <= 0 )
        {
            Application.LoadLevel("end");
        }
    }
}

//This script displays the HUD, with current weapon, ammo left, Health, Shield, and score
var GUIskin:GUISkin; //The skin gui we'll use
var CountdownTextures:Texture2D[];
private var CountdownTextureIndex:int = 0;

function OnGUI()
{
    GUI.skin = GUIskin; //The skin gui we'll use

    if ( CountDownToStart > Time.timeSinceLevelLoad )
    {
        GUI.DrawTexture(Rect( (Screen.width - CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)].width) * 0.5, (Screen.height - CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)].height) * 0.5, CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)].width, CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)].height), CountdownTextures[Mathf.Round(Time.timeSinceLevelLoad)]); //Draw the HUD texture
    }
}

Upvotes: 2

Views: 61

Answers (1)

Rafal Wiliński
Rafal Wiliński

Reputation: 2390

Mathf.Round(Time.timeSinceLevelLoad) is just bigger CountdownTextures array size.

Maybe try adding this:

Debug.Log("Index: "+Mathf.Round(Time.timeSinceLevelLoad)+", Array size: "+ CountdownTextures.Length);

And you should see that index is bigger than size.

You can also add kind of safety check inside if, something like this:

if(Mathf.Round(Time.timeSinceLevelLoad) < CountdownTextures.Length && <your second statement>)

Upvotes: 2

Related Questions