- Home /
Adding a texture to array textures?!
Hey guys! Im trying to make a thing where in one script I make an array for button(skill bar), so if I enter 3 there will be 3 buttons with their own textures, and in another script I want to add a button and then assign a texture to that new button. It keeps telling me Array index out of range! What should I do?
Skill bar Script: #pragma strict
public var count : int;
public var SkillsNum : boolean[] = [];
public var SkillsPos : Rect[];
public var SkillsNames : String[];
public var SkillsTexture : Texture2D[];
function Start () {
}
function Update () {
SkillsPos = new Rect[count];
SkillsNum = new boolean[count];
SkillsNames = new String[count];
SkillsTexture = new Texture2D[count];
}
function OnGUI () {
for(var i : int = 0; i < SkillsNum.length; i++){
SkillsNum[i] = GUI.Button(Rect(Screen.width - (Screen.width * 0.6) + 52 * i, Screen.height - (Screen.width * 0.05), 50,50),SkillsTexture[i]);
if(SkillsNum[i]){
}
}
}
other script(part of it, rest is the EquipmentEffect from Brakeys):
var texture : Texture2D;
Player.GetComponentInChildren(SkillBar_02).count += 1;
Player.GetComponentInChildren(SkillBar_02).SkillsTexture[i] = texture;
"count" determines how many buttons there are.
Answer by zharik86 · Sep 03, 2014 at 08:02 AM
On how many I see, you change the array size, but you don't change an array. And it isn't clear that for the variable "i" in the second piece of a code. I will a little change your code and I will add comments. First script by name "SkillBars_02":
#pragma strict
public var count: int;
public var SkillsNum: boolean[];
public var SkillsPos: Rect[];
public var SkillsNames: String[];
public var SkillsTexture: Texture2D[];
function Start () {
//Here initialization your arrays. It isn't necessary to create each frame them as Update ()
SkillsPos = new Rect[count];
SkillsNum = new boolean[count];
SkillsNames = new String[count];
SkillsTexture = new Texture2D[count];
//After you fill arrays with their values
}
function Update () {
}
function OnGUI () {
for(var i: int = 0; i < SkillsNum.Length; i++) {
SkillsNum[i] = GUI.Button(Rect(Screen.width - (Screen.width * 0.6) + 52 * i, Screen.height - (Screen.width * 0.05), 50,50),SkillsTexture[i]);
if(SkillsNum[i]) {
}
}
}
//Create new function, which addind new texture to array
function myAddTexture(tpTex: Texture2D, tpRect: Rect, tpName: String) {
Debug.Log("Value count = " + count + " value Length = " + SkillsTexture.Length);
//Values "count" and "Length" shall be equal
count++; //increase value of count elements of arrays
//create temp array for new textures
var tpSkillsTexture: Texture2D[] = new Texture2D[count];
//create temp array for other elemets skills
var tpSkillsPos: Rect[] = new Rect[count];
var tpSkillsNum: boolean[] = new boolean[count];
var tpSkillsNames: String[] = new String[count];
//Fill arrays previos data and last index new data
for(vat i: int = 0; i < count - 1; i++) {
Debug.Log("Store element i = " + i);
tpSkillsTexture[i] = SkillsTexture[i];
tpSkillsPos[i] = SkillsPos[i];
tpSkillsNum[i] = SkilsNum[i];
tpSkillsNames[i] = SkillsNames[i];
}
//And add new skill
tpSkillsTexture[count - 1] = tpTex;
tpSkillsPos[count - 1] = tpRect;
tpSkillsNum[count - 1] = false;
tpSkillsNames[count -1] = tpName;
//Now we remember new reference
SkillsTexture = tpSkillsTexture;
SkillsPos = tpSkillsPos;
SkillsNum = tpSkillsNum;
SkillsNames = tpSkillsNames;
}
And second script(you in it shall have texture, a rect and a name of a new skill), more precisely only its part.
Player.GetComponentInChildren(SkillBar_02).myAddTexture(texture, Rect(50, 100, 50, 50), "YourSkillName");
In the second part it isn't necessary to change count value. It changes as myAddTexture(). Rect and Name you can set any which you think the correct. I hope that it will help you.
Hey! thanks for replying! :D Damn, I seem to have a load of errors lol. Anyways, you had some typos in the variable names(you wrote Skill, ins$$anonymous$$d of Skills, so u just forgot "s" for every variable) I fixed that, and now every time I try to equip something so it adds me a new button, it says Array index out of range, and it points to the function myAddTexture. Inside that function tpSkillsTexture[i] = SkillsTexture[i], that is the line where the error leads to, and are u sure that it suppose to be tpSkillsTexture[count - 1] = tpTex ? i think it should be a + ins$$anonymous$$d of -, i not sure, im not a good programmer, so can you please help again? :D
@DrakeDiablo Thanks for an error with syntax. I corrected the answer. Now I will write you the basics work of arrays and their indexes(write on JavaScript):
//For example, we need to create int array on 14 elements
var temp: int[] = new int[14];
//Count elements in our array is 14, but index element from 0 to 13.
var b: int = temp[0]; //variable "b" is equal first element of array
var c: int = temp[13]; //variable "c" is equal last element of array
var d: int = temp[14]; //Error: Array index out of range
var e: int = temp.Length; //in our case variable "e" is equal 14
And now about an error. $$anonymous$$ost likely you somewhere else change "count" value. Check that it changed only as myAddTexture(). And for clearing up of errors you can use Debug.Log (). It will allow you to see messages in the console. I will a little add the answer.
Your answer
Follow this Question
Related Questions
array problem 1 Answer
Error CS0029 Help? (Screenshot of Exact Error) 1 Answer
C# array not behaving as expected 0 Answers
The index value is not starting from 0 0 Answers
IndexOutOfRangeExeption - Array index is out of range 2 Answers