- Home /
Array out of range!
Hello!
im trying to make a script that shows amount of buttons(the amount depends on the array). So if I enter 2 for Skills, there will be two buttons on the screen, this is just a start to it, so I havent actually added, a button adder thing, but right now I should have 1 button on my screen, but I dont, it always give me an error "Array out of range" no matter what I do. What should I do? Thanks
public var SkillPos : Rect[];
public var Index : int;
public var Skills : int[];
public var Names : String[];
function Start () {
}
function Update () {
}
function OnGUI () {
GUI.Button(SkillPos[Index], Names[Index]);
}
If this is the only code you've got, both the SkillPos and Names arrays are empty. Thus trying to access any of their elements will throw this error.
It's best to know the size of an array prior to creating it. Furthermore, in situations which require arrays to grow or shrink, a List is almost always preferable. Not sure if lists are available in JS, but you should consider switching to C# regardless - it's far superior.
Answer by BMayne · Aug 31, 2014 at 01:29 AM
Hey There,
Your arrays have no size and you are looking for the first element. Think of it this way.
string myArray[3]{ "First", "Second", "Third" }
Here is an array with three elements. If I try to access element zero like you do above you will get the following result.
Debug.Log(myArray[0]);
--------- Fake Log ---------
First
----------------------------
Now if I set the array like you did you will get this
string myArray[];
Debug.Log(myArray[0])
--------- Fake Log ---------
Out of range error!
----------------------------
You can't use whats not there :)
I hope that helps.
Regards,
Hey, thanks for replying, I changed my code again and same problem...
#pragma strict
public var SkillsNum : boolean[];
public var SkillsPos : Rect[];
public var SkillsNames : String[];
public var SkillsTexture : Texture2D[];
function Start () {
SkillsPos = new Rect[1];
SkillsNum = new boolean[1];
SkillsNames = new String[1];
SkillsTexture = new Texture2D[1];
SkillsPos[0] = Rect(5,5,5,5);
}
function Update () {
}
function OnGUI () {
for(var i : int = 1; i <= SkillsNum.length; i++){
SkillsNum[i] = GUI.Button(Rect(52 * i, 10, 50,50),"");
if(SkillsNum[i]){
}
}
}
for(var i : int = 1; i <= SkillsNum.length; i++)
This code should be:
for(var i : int = 0; i < SkillsNum.length; i++)
Arrays start from "0" and go to "lenght - 1".
I just changed it to 0, still says array index out of range :(
Change it to 0 and make sure it goes to "i < SkillsNum.length" not "i <= SkillsNum.length". Notice ! To "<" not "<=".
YAY!!! man ure the best, thanks for help!! one question, do you know where can i find tutorials on this stuff? because I need to make buttons appear in certain position on the screen, and that position depends on the rect array. :D thanks again!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Arrange ints from biggest to smallest and display them in a table 1 Answer
Bringing window to back GUI.BringWindowToBack() 0 Answers
GUISttyle NullReferenceException error. what am I missing ? 1 Answer
GUI error scripting help! 2 Answers