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]);
}

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 :slight_smile:

I hope that helps.

Regards,