Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by illfusebox · Dec 15, 2011 at 01:27 AM · arrayarraysfor-loopgui-button

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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;
 }

}

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image illfusebox · Dec 15, 2011 at 02:01 PM 0
Share

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!

avatar image jahroy · Dec 15, 2011 at 04:07 PM 0
Share

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)

avatar image illfusebox · Dec 16, 2011 at 01:52 PM 0
Share

sir jahroy its working but its no previous button can u $$anonymous$$ch me how to do it with this code?

avatar image NikSpace · Dec 16, 2011 at 04:08 PM 0
Share

simple by dividing from the Array "currentTex --;" with an other button.

avatar image illfusebox · Dec 16, 2011 at 04:43 PM 0
Share

sir nikspace can u guide me sir how to do that can u please have a sample code?

Show more comments
avatar image
1

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Dec 16, 2011 at 09:28 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges