I’m trying to loop through a list and add the values to a scroll view content as a text object. Here is my code but not working currently/not finished. Not sure how to add objects to the scrollview.content.
public GameObject _Content;
private Text _Text;
private List<string> lst = new List<string>();
// Use this for initialization
void Start ()
{
//Logged In Redirect
//if (!User_Data._Logged_In)
//{
// SceneManager.LoadScene("Log_In");
//}
//else
//{
//}
}
private void Build_Promotional_Codes()
{
int index = 0;
lst.Add("Promotional code 1");
lst.Add("Promotional code 2");
lst.Add("Promotional code 3");
lst.Add("Promotional code 4");
lst.Add("Promotional code 5");
foreach(string str in lst)
{
_Text.text = lst.ToString();
//_Content.
}
}
You need to assign your list strings values to text gameobjects and then parent (use transform.SetParent) those gameobjects to the content object of the scrollview.
The problem you’re having is that you only have one text component(that i assume is already in the scene, and you’re getting a reference to) and you’re overriding the text that it has, you need to either have a new component for every line (like @Munchy2007 said) and set it’s text to what ever, or keep it as one but add the texts instead of overriding it and use new line mod ( /n or System.Enviroment.NewLine)
The better option is to have a new object for each element you want to display, imo.
Have a blank-slate object that have all the setting needed(like font, size, color, w/e), have it as a prefab, instantiate it, parent it to the content, set its text.
I can provide a some-what related sample code when I get back from work.(from one of my games shop UI)
Ooooo hh right right right yes I need to have it create a new instance of the Text object each time. I’m sorry I over looked that detail. Yes a code sample would be great. Thank you very much for your help.
a bit late, but here’s the shop that fills my UI shop
void PopulateShop(){
Part[] parts = Resources.LoadAll ("Parts", typeof(Part)).Cast<Part> ().ToArray (); //get all the elements you want to display
for (int i = 0; i < parts.Length; i++) {
GameObject go = (GameObject)Instantiate (UIPartShop, shopContent.transform, false); //Spawn a 'blank' object and parent that to the content
UI_Part uip = go.GetComponent<UI_Part> (); //Get the reference to the script on the object
uip.Setup (parts [i]); //Set it up with the information from parts[i]
go.GetComponentInChildren<Button> ().onClick.AddListener (uip.Buy); //Find it's button and add the function "Buy" to it
}
}
Not sure I follow your logic. Do you have a tut or something I can review? I have tried watching several tuts on this topic and everyone seems to be adding their objects by hand. I think what I need is more of a Data Table or list box. What I will be doing is pulling down a data table from my data base. It will have a list of Promotional Codes the user can use. I’d like to use a button so they can click on it and it auto fills a input field for them but a simple text list would be fine too. I just need something dynamic to build a list.
In a nutshell I just need to know how to build the object via script or dynamically and add it to the scroll view content. If any one has a tut on how to do this that would be great. Thanks
came up with this logic. Still building it but you get the idea:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Load_Promotional_Code : MonoBehaviour
{
public GameObject _txtPromotionalCode;// This is our prefab object that will be exposed in the inspector
private List<string> _lstPromotionalCodes = new List<string>();
// Use this for initialization
void Start ()
{
User_Data._Current_Scene = "Promotional_Code";
//Logged In Redirect
//if (!User_Data._Logged_In)
//{
// SceneManager.LoadScene("Log_In");
//}
//else
//{
//}
}
void Populate()
{
GameObject obj; // Create GameObject instance
int count = 0; //get this value from datatable count returned from database
for (int i = 0; i < count; i++)
{
// Create new instances of our prefab until we've created as many as we specified
obj = (GameObject)Instantiate(_txtPromotionalCode, transform);
obj.GetComponent<Text>().text = "";
}
}
}