Can't seem to get my Texture2d array to work correctly. Maybe someone might know what's wrong. It's going to be for keeping track of equipped items. Maybe there's a better way of doing this. I keep getting indexOutOfRange exception. Code (csharp): //main code //inventory icons var weaponSlot : Texture2D[] = new Texture2D[11]; function OnGUI () { //top item row if (GUI.Button(Rect((Screen.width/2) - 796,(Screen.height/2) - 297,50,50),weaponSlot[0])) { } if (GUI.Button(Rect((Screen.width/2) - 717,(Screen.height/2) - 297,50,50),weaponSlot[1])) { } if (GUI.Button(Rect((Screen.width/2) - 632,(Screen.height/2) - 297,50,50),weaponSlot[2])) { } if (GUI.Button(Rect((Screen.width/2) - 547,(Screen.height/2) - 297,50,50),weaponSlot[3])) { } if (GUI.Button(Rect((Screen.width/2) - 457,(Screen.height/2) - 297,50,50),weaponSlot[4])) { } //bottom item row if (GUI.Button(Rect((Screen.width/2) - 796,(Screen.height/2) - 207,50,50),weaponSlot[5])) { } if (GUI.Button(Rect((Screen.width/2) - 717,(Screen.height/2) - 207,50,50),weaponSlot[6])) { } if (GUI.Button(Rect((Screen.width/2) - 632,(Screen.height/2) - 207,50,50),weaponSlot[7])) { } if (GUI.Button(Rect((Screen.width/2) - 547,(Screen.height/2) - 207,50,50),weaponSlot[8])) { } if (GUI.Button(Rect((Screen.width/2) - 457,(Screen.height/2) - 207,50,50),weaponSlot[9])) { } } //script attached to each item prefab var item_attackPower : float; var item_type : int; var item_icon : Texture2D; //item_type. 0 = weapon, 1 = helm, 2 = shoulders function Start () { playerStats.attackPower += item_attackPower; itemType(); } function Update () { } function itemType () { var otherScript: playerStats = gameObject.Find("player").GetComponent(playerStats); switch (item_type){ case ("0"): otherScript.weaponSlot[0] = item_icon; break; case ("1"): otherScript.weaponSlot[1] = item_icon; break; case ("2"): otherScript.weaponSlot[2] = item_icon; break; } }
Public variables get their values from the inspector. So don't do this sort of thing: Code (csharp): var weaponSlot : Texture2D[] = new Texture2D[11]; Since that implies there should be 11 items in the array, but it's not defined by the code, it's defined by what you put in the inspector. Just do this: Code (csharp): var weaponSlot : Texture2D[]; If you want to have 11 items as the initial default value, put that in the Reset function: Code (csharp): function Reset () { weaponSlot = new Texture2D[11]; } That way it will be set to 11 entries in the array by default and when using the Reset menu item. However the actual value is always controlled by the inspector. If you get an out of range error, that means you're referring to items that are not in the array. So if you say weaponSlot[10] and get the error, that means you didn't put at least 11 items in the array. --Eric
I just came back to post that that's what I just figured out haha. I'm having an issue with my switch statement though for some reason. No matter what number I have my item_type set to nothing is being called. I did a Debug and the function is still being called at the Start.