- Home /
GUI button image next
can anyone help me, how to create a button and a box and it compose of image and when u press the next button it loads another button.. please help me i want it badly. thanks for your cooperation and more power!
Answer by jahroy · Dec 15, 2011 at 02:56 AM
Here's the basic idea.
To use this you will have to populate the array of images in the Inspector.
This code draws a texture as a label and then draws a button.
If the button is clicked, the offset into the array of images is incremented.
If it hits the end of the list, it's reset to zero.
This code will cause a bunch of errors if you don't initialize the array of textures.
/* initialize the array of images by using the Inspector.
var imageArray : Texture2D []; var currentTex : int = 0;
var texRect : Rect = Rect(200, 200, 100, 100); var buttRect : Rect = Rect(200, 300, 100, 40);
function OnGUI () { GUI.Label(texRect, imageArray[currentTex]);
if ( GUI.Button(buttRect, "Next") ) {
nextImage();
}
}
function nextImage () : void { currentTex ++;
if ( currentTex >= imageArray.length ) {
currentTex = 0;
}
}
sir, im a beginner in here in unity can u please guide me some no error code just a simple one?.. thanks sir! more power!
The best way to learn is to figure out how to fix the errors yourself.
$$anonymous$$y answer is just meant to be a starting point.
Good luck!
(note that I explain that the array must be initialized in the Inspector 3 times in the first 7 lines of my answer)
sir jahroy its working but its no previous button can u $$anonymous$$ch me how to do it with this code?
simple by dividing from the Array "currentTex --;" with an other button.
sir nikspace can u guide me sir how to do that can u please have a sample code?
Answer by NikSpace · Dec 16, 2011 at 09:00 PM
/* initialize the array of images by using the Inspector.
var imageArray : Texture2D [];
var currentTex : int = 0;
var texRect : Rect = Rect(200, 200, 100, 100);
var buttnextRect : Rect = Rect(200, 300, 100, 40);
var buttprevRect : Rect = Rect(200, 400, 100, 40);
function OnGUI ()
{
GUI.Label(texRect, imageArray[currentTex]);
if ( GUI.Button(buttnextRect, "Next") ) {
nextImage();
}
if ( GUI.Button(buttprevRect, "Previous") ) {
previousImage();
}
}
function nextImage () : void
{
currentTex ++;
if ( currentTex >= imageArray.length ) {
currentTex = 0;
}
}
function previousImage () : void
{
currentTex --;
if ( currentTex == 0 ) {
currentTex = imageArray.length;
}
}
You actually want it to be:
if ( currentTex < 0 ) {
currentTex = imageArray.length - 1;
}
Arrays indices start at zero. Zero is a valid offset into the array. It represents the first element.
imageArray[imageArray.length], however, will throw an exception.
If you have an array with 8 elements, the valid indices are 0-7.
If you try to access element 8, you will get an array index out of range exception.
This is why you would write a for loop that accesses every element of the array like this:
for ( var i = 0; i < imageArray.length; i ++ ) {
print(imageArray[i].name);
}
This loop starts a variable named i at zero and executes over and over until i is no longer less than the length of the array. If there are 8 elements in the array, the last time the loop will execute is when i is equal to 7. Once it is equal to 8, the for loop no longer executes, because its middle expression no longer evaluates to true.
Note that the value of i increses every time because of the third expression of the for loop, which says: i ++
All of this stuff can be read in the documentation, online, in books, etc....
Your answer
Follow this Question
Related Questions
IndexOutOfRange error on iPhone, but not on PC/Player 0 Answers
Null reference when editing an array in a for loop 1 Answer
How Do I Add An Instantiated Object To An Array? 3 Answers
How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers
Why does my array only execute the last of the array? 1 Answer