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
0
Question by fmaciasjimenez · Apr 15, 2014 at 11:23 PM · iphonebuttonsguistylegui.buttoninstructions

Accessing a particular GUI.Button in code after it has been created

I am working on a project (iOS) that requires me to have access to a particular button. I am creating several buttons using a FOR loop and using an array as its origin. Each array element has several variables that I use within the IF declaration of the button so I can give buttons functionality.

What I want to do are two things:

1) To be able to tap a particular button and change its background without changing the background of all the other buttons. I thought of using a toggle button, but it doesn't work for what I am trying to accomplish. I tried adding a change of GUIStyle to the IF statement but it changes all other buttons with it.

2) I need to implement a Play functionality that basically "steps through" every button, each button changing background when it's doing something and executing its function when it's called.

Is there a way I can "tag" the buttons for me to access them individually later on in code? Is there a way to assign a name to a button to access it that way? Can you think of a way to achieve these things in a simpler way?

Thank you very much for the help!

Comment
Add comment · Show 1
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 Anni2D · Aug 12, 2014 at 07:02 AM 0
Share

I wish I could upvote. Exactly same Q what I want to search!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Apr 16, 2014 at 12:33 AM

I'm not sure of your exact requirements, and I'm not an expert on the GUI system since I use EZGUI for my interface word. But you can use an index to color some specific button. Here is an exmple script. Attach to an empty game object. You can change the value of indexToColor to change which button (zero based index) gets a color.

 #pragma strict
  
 private var indexToColor = -1;
 private var rect = Rect(0,0,100,50);
 private var dist = 60;
 private var strings = ["One", "Two", "Three", "Four", "Five"];
 private var color = Color.red;
  
 function OnGUI ()  {  
     for (var i = 0; i < strings.Length; i++) {
        var r = rect;
        r.y = i * dist;
        var saveColor : Color;
        if (i == indexToColor) {
          saveColor = GUI.color;
          GUI.color = color;
        }
        if (GUI.Button(r, strings[i])) {
          Debug.Log("The "+i+"th button was pressed");
          indexToColor = i;
        }
        if (i == indexToColor) {
          GUI.color = saveColor;
        }
     }
 }
 
   
Comment
Add comment · Show 7 · 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 fmaciasjimenez · Apr 17, 2014 at 02:46 AM 0
Share

Hi Robert, thank you for your answer! This definitely proves to be extremely helpful in what I'm trying to accomplish, although when I try out the script it always seems to color the "Two" button red regardless of the indexToColor value and I'm not sure why. Any idea why that could be?

avatar image robertbu · Apr 17, 2014 at 03:19 AM 0
Share

You were probably trying to change the value in the script. Since 'indexToColor' is a public variable, any code initialization of public variables is only used at the time the script is attached. After that only the value in the inspector matters.

I change the example code. I made 'indexToColor' private, and now it changes the color of the button last clicked.

avatar image fmaciasjimenez · Apr 17, 2014 at 03:55 AM 0
Share

You're absolutely right. I wrongly assumed it was a private variable. Thank you!

avatar image fmaciasjimenez · Apr 18, 2014 at 02:09 AM 0
Share

I have one more question. I'm trying to do exactly what you just did but with GUIStyles ins$$anonymous$$d of colors so I can use images ins$$anonymous$$d of colors. It seems to me that it should be somewhat similar to the code, but the only thing I'm having trouble with is getting the equivalent to the GUI.color in the non-button IF statements. I'm assu$$anonymous$$g I just have to add what the current GUIStyle is, but I'm not quite sure how I can dynamically get the current GUIStyle. Any suggestions? (Sorry if this is obvious or too ambiguous, but I'm somewhat of a beginner still and often overlook things).

avatar image robertbu · Apr 18, 2014 at 03:33 AM 0
Share

Since I use EZGUI, I don't have a deep understand of Unity's GUI, so there may be better ways to use GUIStyles. Below is a bit of code that uses GUIStyles. I don't use the current style. Ins$$anonymous$$d there are two new styles: 'styleNormal' and 'stylePressed'. You are responsible for initializing the textures and the font in the Inspector. Just open a style and drag and drop the textures into the appropriate slots. For my test, I also change the font and font alignment as well as the textures. Here is the new code:

 #pragma strict
  
 private var indexToColor = -1;
 private var rect = Rect(0,0,100,50);
 private var dist = 60;
 private var strings = ["One", "Two", "Three", "Four", "Five"];
 private var color = Color.red;
 
 public var styleNormal : GUIStyle;
 public var stylePressed : GUIStyle;
  
 function OnGUI ()  {  
     for (var i = 0; i < strings.Length; i++) {
        var r = rect;
        r.y = i * dist;
        var saveColor : Color;
        
        var style = (i == indexToColor) ? stylePressed : styleNormal;
 
        if (GUI.Button(r, strings[i], style)) {
          Debug.Log("The "+i+"th button was pressed");
          indexToColor = i;
        }
     }
 }
Show more comments

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnGUI/iPhone - cannot touch TWO+ buttons at the same time...? 3 Answers

Optimizing OnGUI - Too many gui elements? 2 Answers

Remove Button Border 3 Answers

GUI: Overlapping and Box size collision 1 Answer

Recreating iPhone Home Screen in Unity 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