Changing text values every time you instantiate the button

Hi I am using MasterServer.RequestHostList so I was wondering how to get the values I get from that and put it into an object I have instantiated. For example I have a object that has a set of text, it is supposed to show the server name, I want it show the server’s name for each instantiated object. One server could be “derpyface” and the other could be “hellllooooo” I want it so that it creates a list of objects with each one showing the new server’s name.

I understand I am pretty bad at explaining so just comment a question, if you want a more in depth explanation.

Hi @Kingofweirdos
I took code from MasterServer.RequestHostList Scripting API and modified it :slight_smile:

In inspector:

  1. Create empty GameObject and call it for Ex. “ButtonManager”.

  2. Create UI.Canvas so you can place UI.

  3. Create Another UI.Canvas and call it “ButtonCanvas”, this will be root for your buttons.

  4. Create one button and call it “ServerButtonPrefab”. Set it as your first server button, then make prefab of it and delete it.

  5. Place my script on ButtonManager object.

  6. In inspector set Button prefab to recetly created ServerButtonPrefab

  7. Set target Area to ButtonCanvas

  8. Set preferable offset (how far away will be buttons created)

  9. Hit play and let me know if everything is doing alright :slight_smile:

  10. CODE:

    using System.Collections;

    using UnityEngine.UI;

    public class ExampleClass : MonoBehaviour {
    public Button buttonPrefab;
    public Canvas targetArea;

       public float offset;
    
       void Awake() {
       	MasterServer.ClearHostList();
       	MasterServer.RequestHostList("LarusTest");
       	//TEST:
       	/*createButton ("HelloBOB", 0);
       	createButton ("Hello", 1);
       	createButton ("BOB", 2);*/
       }
       void Update() {
       	if (MasterServer.PollHostList().Length != 0) {
       		HostData[] hostData = MasterServer.PollHostList();
       		int i = 0;
       		while (i < hostData.Length) {
       			Debug.Log("Game name: " + hostData*.gameName);*
    
  •  		//create button*
    

_ createButton(hostData*.gameName, i);_
_
i++;_
_
}_
_
MasterServer.ClearHostList();_
_
}_
_
}*_

* void createButton(string text, int index){*
* //create button*
* Button newButton = Instantiate(buttonPrefab);*
* //sets it transform*
_ /NOTE:
* button will inherit y position of its prefab, making it easier to place_

_ * /_
_
newButton.transform.SetParent (targetArea.transform, false);
_
* newButton.transform.position = new Vector3 (newButton.transform.position.y,*
_ newButton.transform.position.y-indexoffset, newButton.transform.position.z);_
_
//set text*_
* newButton.GetComponentInChildren().text = text;*
* }*
}
PS: I would recommend using dropdown instead of buttons.