- Home /
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);
}
}
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";
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.
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;
}
}