I’m making an array filled with some gameObjects all tagged as “Spells”.
What is happening is that always the first element is shown last when iterating through the array.
Here is the code:
var Pre_Spells : GameObject[];
private var i : int = 0;
private var item_pos : int = 0;
function Start() {
fillArray();
}
function OnGUI() {
for(i=0; i<Pre_Spells.length; i++) {
GUI.Box(Rect(5,35+item_pos,390,250), GUIContent(Pre_Spells<em>.GetComponent(Spell).Spell_Name,Pre_Spells*.GetComponent(Spell).Spell_Icon));*</em>
if(GUI.Button(Rect(5,200+item_pos,150,50), “Check”)) {
Debug.Log(i);
}
item_pos = 250 * i;
}
}
function fillArray() {
Pre_Spells = GameObject.FindGameObjectsWithTag(“Spells”);
}
So, for example, Pre_Spell is filled like this:
Pre_Spell[0] = spell0
Pre_Spell[1] = spell1
Pre_Spell[2] = spell2
But when iterating the array it shows
spell1
spell2
spell0 → it was the first element, but shown last!
The gui.button “check” actually shows the correct array position.
Is this a normal behaviour? Am i doing something wrong?
any help is much appreciated.