Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 daccithechopper · Nov 24, 2016 at 07:22 AM · list

Displaying the whole list in a single Text

I'm a beginner to Unity, and want to make one sheet of input history, which is expected to show as (1, 2, 1, 2, 3,....) endlessly. But I can only pick one of numbers from the list, stating by mylist[1] in order. Is there any way I can state the whole list, and display as a Text?

 GameObject counter;

 public List<int> brain = new List<int>();

 // Use this for initialization
 void Start () {
     this.counter = GameObject.Find("Counter");

 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         brain.Add(1);
            }
     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         brain.Add(2);
     }
     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         brain.Add(3);
     }
     counter.GetComponent<Text>().text = brain[0].ToString(); 

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

Answer by Mister-Mortal · Nov 24, 2016 at 08:35 AM

Use this method to convert your list to string:

 private string ListToText(List<int> list)
 {
     string result = "";
     foreach(var listMember in list)
     {
         result += listMember.ToString() + "\n";
     }
     return result;
 }

Use it like this:

 counter.GetComponent<Text>().text = ListToText(brain);
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 daccithechopper · Nov 24, 2016 at 11:43 AM 0
Share

Thank you so much! So, you could convert the list to string! This did not work on my script, so I'll study on "ListToText" because I will need it! :)

avatar image Mister-Mortal daccithechopper · Nov 24, 2016 at 03:29 PM 1
Share

@daccithechopper I forgot to add semicolon after "\n". Should do the job now.

avatar image
2

Answer by BackslashOllie · Nov 24, 2016 at 08:36 AM

What you need is a for or a foreach loop.

Please see below a quick example of using a foreach loop

 GameObject counter;
     public List<int> brain = new List<int>();
     // Use this for initialization
     void Start()
     {
         this.counter = GameObject.Find("Counter");
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             brain.Add(1);
         }
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             brain.Add(2);
         }
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             brain.Add(3);
         }
 
         counter.GetComponent<Text>().text = ""; //Clear the text
         foreach (int item in brain) //Add each item to the text
             counter.GetComponent<Text>().text += item.ToString() + ", ";
     }
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 daccithechopper · Nov 24, 2016 at 11:37 AM 0
Share

Thank you so much! The foreach loop worked just as I wanted. I was blindly just trying to get the whole list in the script, I should have tried to add to the text by myself, like this :)

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

6 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

List woes. (C#) 1 Answer

Show Top 10 Of 1 GameType 2 Answers

Find GameObjects with a certain Script [Solved] 1 Answer

print the whole list to screen 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