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 techboysquared1 · Jun 27, 2013 at 05:30 AM · guibuttons

Can I dynamically create buttons from an arraylist?

In my program I am trying to create buttons from an arraylist; however, I can't seem to get it working. The button is displayed with the correct name whenever I add something to the arraylist useroptions, but whenever I click on the button the action that I define in the curly braces is not triggered. I have the following code in my OnGUI function:

 foreach (string s in userOptions)
         {
             Debug.Log(s);
             if (GUI.Button(new Rect(100, 100, 300, 300), s))
             {
                 Debug.Log ("HERE");
                 npcConversation.reponseText = s;
 
             }
         }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jun 27, 2013 at 05:36 AM

I ran a quick test of your code. It appears to work fine (other than you are placing all the buttons on top of each other). Here is what I did:

 using UnityEngine;
 using System.Collections;
  
 public class Bug20 : MonoBehaviour
 {   
     string[] userOptions = new string[] {"one", "two", "three"};
     void OnGUI ()
     {
         foreach (string s in userOptions)
             {
                 Debug.Log(s);
                 if (GUI.Button(new Rect(100, 100, 300, 300), s))
                 {
                     Debug.Log (s);
                 }
             }
     }
 }

If I click on the button, it outputs text for all three buttons (which I did not expect).

Comment
Add comment · Show 5 · 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 dorpeleg · Jun 27, 2013 at 05:59 AM 0
Share

@robertbu

I'ts probably because they are on top of each other like you said.

So maybe when you click, all of them get clicked.

@techboysquared1

You might wanna make your Rect with a dynamic vars so the buttons wont be on top of each other.

avatar image Tarlius · Jun 27, 2013 at 07:38 AM 0
Share

@robertbu I assume the first Debug.Log is a typo? That code should print out all the button texts even if you don't push the button, I think?

@techboysquared1 I assume by "not triggered" you are checking via the log, so we can safely ignore any possible bug with the responseText logic?

Since you'll be spitting out a hell of a lot of logs, are you certain you didn't just miss the one that said "HERE" in the sea of other logs?

On a side note: As the others have mentioned, your code will put all your buttons in the same place. While you could mess with the Rect, you could also use GUILayout and get Unity to figure it out for you.

avatar image techboysquared1 · Jun 27, 2013 at 08:14 AM 1
Share

Yes. I knew that it would stack them on top of each other. I was only had one button while i was messing with this, so I didn't code it to change button location. Also, I have found my problem apparently it was something to do with the button being stacked on top of a textarea. I moved it off of the text area and everything is fine now.

avatar image Tarlius · Jun 27, 2013 at 08:39 AM 0
Share

How odd that stacking buttons on top lets them all trigger, but a textarea doesn't allow clicks to go through. The wonders of Unity! Thanks for revealing the true cause ;)

I still recommend taking a look at the GUILayout class though, could save you lots of time messing around with Rects.

Edit: $$anonymous$$ight be worth turning that into an answer, btw. That way anyone with a similar problem can quickly see what fixed it for you.

avatar image techboysquared1 · Jun 27, 2013 at 08:43 AM 0
Share

I just realized how horrible my grammar was in that comment. I guess it's because it's 4 A$$anonymous$$, and I'm looking at the GUILayout now.

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

17 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

Related Questions

Tutorials for 2D game GUI 2 Answers

I want to move my cube using GUI buttons? 1 Answer

Only buttons closest to EventSystem (or last in the list under Canvas?) work. 1 Answer

How Can I Create Buttons In World Space and NOT Screen Space 2 Answers

Don't register Mouse Clicks through GUI. 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