[Solved]Next and Previous Images of Array

Holla, i’ve try to do some code, and with my not-so-good-algorithm-brain it comes up on a dead end… so, here’s my code.

#pragma strict
var images : Texture2D[];

function OnGUI () {

	var i : int;
	GUI.DrawTexture(Rect(200,50,100,100),images*);*
  • if(GUI.Button(new Rect(210,300,100,50),“next texture”)){*
  •  print(i);*
    
  •  i++;*
    

_ GUI.DrawTexture(Rect(200,50,100,100), images*);_
_
print(i);_
_
}_
_
if(GUI.Button(new Rect(100,300,100,50),“previous texture”)){_
_
i–;_
_ GUI.DrawTexture(Rect(200,50,100,100), images);
print(i);*_

* }*
}
i know that my code is so lame, and weird… *haha
but i think i need something like to repaint the gui drawtexture… the i always saying 1 and 0 only, everytime i press Next texture button, it shows up 0 and 1, then press it again, 0 and 1 again… doesn;t the counter should go up? or because i put the var and GUI.DrawTexture on the top of the if’s? Thanks for further help, some poniter will be appreciated :slight_smile:

It looks like this is because you are declaring i inside of OnGUI. Each time OnGUI is run, i is given the default value of 0 when it is declared. Try making i a class variable by declaring it outside of any functions.

#pragma strict
var images : Texture2D[];
var i : int = 0;
 
function OnGUI () {
    GUI.DrawTexture(Rect(200,50,100,100),images*);*

if(GUI.Button(new Rect(210,300,100,50),“next texture”)){
i++;
print(i);
}
if(GUI.Button(new Rect(100,300,100,50),“previous texture”)){
i–;
print(i);
}
}

Once in a while it will segfault… Add something like this:

if (i > 0)
    i--;

if (i < images.Length - 1)
    i++;