- Home /
Array index is out of range (Javascript)
OK, so i have this simple inventory script and for some reason i get this error when i hit the play button and then try to click.
IndexOutOfRangeException: Array index is out of range. Inventory02.Update () (Assests/script/inventory02.js:11
From the error i assume it is on line 11 but there is nothing with an array on that line! I really need some help on this!
Error on line 11...i think
#pragma strict
var Player : Transform;
var material : int;
var Static : int = 1;
var ItemTexture : Texture;
var NewText : Texture[];
function Start()
{
ItemTexture = NewText[material];
}
function Mining(Character : Transform)
{
Player = Character;
Player.SendMessage("ADD",material);
Player.SendMessage("ADDTexture",ItemTexture);
Static -=1;
}
function Update()
{
if(Static <=0)
{
Destroy(gameObject);
}
}
Line 11 is most defintely an array. You are assigning ItemTexture to be the array element of NewText[material] where material is your int/array index.
http://unity3d.com/learn/tutorials/modules/beginner/scripting/arrays
Perhaps, you have not assigned your NewText:Texture[]
from the inspector. When you assigned Texture[] from the inspector, you should be able to access the array.
Further note, your code is really difficult for me to read.
var Player : Transform;
I had to stop a little while to see which one is the variable and which one is the type. But, the following is a lot easier to read.
var player : Transform;
I dont understand why i am getting the error though, i made sure all textures were assigned in the inspector.
it is automatically set to 0 in the unity inspector... but i can change it if i want. Should it be the same as another variable?
Answer by VIPINSIRWANI · Apr 14, 2014 at 09:01 AM
Once try with Replace line number 11 with ItemTexture = NewText[0]; and try.
hi i add your script in my project its working fine here....Once restart unity.
Your answer
