Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ThumbStorm · Sep 26, 2011 at 02:53 PM · guibuttondynamic

Is it possible to create a button dynamically?

Hello, I'm creating a function that will be used to add names to a list. Multiple names can be added and the names will be displayed on buttons so the user can interact with them. I know how to make it all work but instead creating a certain number of buttons at the start of the level, is it possible to create a button dynamically as needed? thanks

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
Best Answer

Answer by save · Sep 26, 2011 at 03:02 PM

Yes it's fully possible! The solution depends on if you're doing a button in the GUI or the scene.

In the GUI throw it into a for-loop where you check how many input-lines is available. Then offset it each iteration.

In the scene you can instantiate an object that has a OnMouseDown-function.

Comment
Add comment · Show 4 · 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 ThumbStorm · Sep 26, 2011 at 03:11 PM 0
Share

Awesome! I was just about to try that, but how would i access each button? I'm using GUI btw.

avatar image blitzen · Sep 26, 2011 at 03:22 PM 1
Share

var yourNamesList:Array=new Array(String);//populate this with your names as you described.
var toolbarSelected:int=-1;
//Either with a toolbar:
toolbarSelected=GUI.Toolbar(Rect(0,0,Screen.width,Screen.height),toolbarSelected,yourNamesList.ToBuiltin(String));
//Or splotch them down manually:
for(var i:int=0;i < yourNamesList.length;i++)GUI.button(Rect(0,i*20,20,10),yourNamesList[i]);
avatar image ThumbStorm · Sep 26, 2011 at 03:54 PM 0
Share

@blitzen I used your "for" method and it creates the buttons like a charm. How could I run a function from one of the newly created button? example: if (GUI.Button(Rect(0,i*20,20,10),yourNamesList[i])){ GetName(yourNamesList[i]); } thanks

avatar image blitzen · Jan 04, 2013 at 06:22 AM 0
Share

@ThumbStorm: That's exactly how you'd do it; you just answered your own question.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GUI.Label positioning for many device resolutions 1 Answer

How can I dynamically create scene ? 1 Answer

GUI, making main page lead into an instructions page 2 Answers

How to get Rect from scene Button ? 1 Answer

Unity 5 GUI system 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