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 /
  • Help Room /
avatar image
0
Question by SamPav · Oct 05, 2015 at 07:47 PM · uilistlists

problem with loading list of strings on buttons

This might be a noob question, but I cant figure this out. I have a canvas with 5 buttons and I am trying to load a list of strings (names) on those buttons. It is quite easy to change the text of each button to particular name, where I am lost is how to load all five names on these five buttons. Here is a code >

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class ListLearning : MonoBehaviour {
 
 List <string> names = new List <string>();
     public GameObject parentCanvas;
     // Use this for initialization
     void Start () {
         names.Add ("Wayne");
         names.Add ("Phil");
         names.Add ("Chris");
         names.Add ("Memphis");
         names.Add ("Adnan");
 
         Debug.Log (names);
 
         AddText ();
     }
     
 
     void AddText () {
 
     
         foreach (Text t in parentCanvas.GetComponentsInChildren<Text>()) 
         {
     t.text = names[1]; // this works, each text is "Phil"
     // t.text = names; //Why doesnt this load the whole list on buttons?
 
         
         }
     }
 }


I have tried several other approaches but I really dont know how to do this ...any help guys? Thanks in advance

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

Answer by fvangeirt · Oct 05, 2015 at 09:24 PM

     void AddText()
     {
 
         int i = 0;
         foreach (Text t in parentCanvas.GetComponentsInChildren<Text>())
         {
             t.text = names[i];
             i++;
         }
     }
Comment
Add comment · 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
0

Answer by SamPav · Oct 05, 2015 at 09:45 PM

Thanks a lot, that works! Although I have no idea why :D Even without explanation, that was very helpful, thanks again :) @fvangeirt

Comment
Add comment · Show 2 · 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 fvangeirt · Oct 05, 2015 at 10:03 PM 0
Share

t.text is a string. names is a list of string. You can not assign a list to a single string variable.

What I do is: Via the foreach you loop over the text strings of your buttons. Parallel you loop with the integer i over the other list of strings to assign.

Sorry, can not explain it better. What exactly do you not understand?

avatar image SamPav fvangeirt · Oct 05, 2015 at 10:10 PM 0
Share

Hm I guess I get it now. I am fairly new to scripting and I have not seen the i++ outside of forloop so that was confusing. Thanks a lot :)

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

How do I add 10 UI text elements to an array or a list...? 1 Answer

How do I modify variables from a different class that belongs to the same C# file? 1 Answer

Why is my listdisplaying that an item was removed? 0 Answers

Layout Group failes to resize children automatically and can't force resize by script when adding child 1 Answer

Find the closest Enemy that has a tag added in a list? 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