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 Senor_Cucumber · May 03, 2016 at 05:03 PM · c#lists

Displaying list in UI

Hello!

I've made this code and I dont know how to display the text in a UI button or text box. Does anyone have any ideas on how to go about displaying elements of my list? Here's my code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 
 
 
 public class Question_String_Script : MonoBehaviour {
 
     public GameObject displayQuestion;
     public GameObject displayAnswer1;
     public GameObject displayAnswer2;
     public GameObject displayAnswer3;
     public GameObject displayAnswer4;
     public bool next = false;
   
     Text dQ;
     Text dA1;
     Text dA2;
     Text dA3;
     Text dA4;
   
     List<string> Question = new List<string>();
     List<string> Answers1 = new List<string>();
       List<string> Answers2 = new List<string>();
     List<string> Answers3 = new List<string>();
     List<string> Answers4 = new List<string>();
     List<int> isCorrect = new List<int>();
   
   
   
   
   
     void Update(){
         if(next){
             next = false;
             int ranNumber = Random.Range(0, Question.Count - 1);
             dQ.text = Question[ranNumber];
             dA1.text = Answers1[ranNumber];
             dA2.text = Answers2[ranNumber];
             dA3.text = Answers3[ranNumber];
             dA4.text = Answers4[ranNumber];
             
         }
         
     
     }
     
     
     void Start(){
         dA2 = displayAnswer2.GetComponent<Text>();
         dA3 = displayAnswer3.GetComponent<Text>();
         dQ = displayQuestion.GetComponent<Text>();
         dA1 = displayAnswer1.GetComponent<Text>();
         dA4 = displayAnswer4.GetComponent<Text>();
     }
     
     
  
     void Awake(){
         
     
         
         //QUESTIONS
         
         
         
         Question.Add("Are the developers of this game freaking amazing?");
         Question.Add("What is Bucket * bucket?");
         Question.Add("");
         Question.Add("");
         Question.Add("");
         
         //Answer1
         
         Answers1.Add("Yes.");
         Answers1.Add("bucketBucket");
         Answers1.Add("");
         Answers1.Add("");
         Answers1.Add("");
         
         
         //ANSWERS2
         
         Answers2.Add("YES HELL YES FUCK YES THEY'RE AMAZING");
         Answers2.Add("Bucket");
         Answers2.Add("");
         Answers2.Add("");
         Answers2.Add("");
         
         
         //Answers3
         
         Answers3.Add("Yea, sure.");
         Answers3.Add("bucketbucket");
         Answers3.Add("");
         Answers3.Add("");
         Answers3.Add("");
         
         
         //Answers4
         
         Answers4.Add("Duh");
         Answers4.Add("bucket");
         Answers4.Add("");
         Answers4.Add("");
         Answers4.Add("");
         
         
         //isCorrect
         
         
         isCorrect.Add(2);
     }
  
 }    
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

Answer by Jessespike · May 03, 2016 at 06:43 PM

Button doesn't have a text property, instead it has a Text component as a child. Setting text on an InputField is the same as Text components.

 // input field
 displayQuestion.GetComponent<InputField>().text = "asdf";

 // button text
 displayQuestion.GetComponentInChildren<Text>().text = "asdf";
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 NonBreaker · May 03, 2016 at 10:19 PM 0
Share

This fact drove me crazy when I first started messing with simple UI components. Not that it was difficult to figure out, it just seems like an oddly obtuse way to present the info. I suppose it gives more flexibility in the end.

avatar image
0

Answer by ThisTestWillDo · May 03, 2016 at 11:56 PM

I'd make the list into an array using the ToArray () method, then join the array into one long string that is set as the value of the Text element. In the inspector, set the Text Of Items variable to the text component you want it to display in.

Here's some example code:

using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;

public class test : MonoBehaviour { public List listOfItems; string[] arrayOfItems; string stringOfItems; public Text textOfItems;

 void DisplayMyList () 
 {
     arrayOfItems = listOfItems.ToArray ();
     stringOfItems = string.Join ("\n", arrayOfItems);
     textOfItems.text = stringOfItems;
 }

}

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Make Lists within a List 0 Answers

How do i activate a function attached to a listed GameObject from the perspective of itself? 1 Answer

Making a bubble level (not a game but work tool) 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