- Home /
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.
Hello! You want to get names of servers and then send them to list of GameObjects. Correct?
1)$$anonymous$$ake sure, that everything else is running ok. Try code from api: (https://docs.unity3d.com/ScriptReference/$$anonymous$$asterServer.RequestHostList.html)
using UnityEngine;
using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour {
void Awake() {
$$anonymous$$asterServer.ClearHostList();
$$anonymous$$asterServer.RequestHostList("LarusTest");
}
void Update() {
if ($$anonymous$$asterServer.PollHostList().Length != 0) {
HostData[] hostData = $$anonymous$$asterServer.PollHostList();
int i = 0;
while (i < hostData.Length) {
Debug.Log("Game name: " + hostData[i].gameName);
i++;
}
$$anonymous$$asterServer.ClearHostList();
}
}
}
2)Do you need any specific way to set name? Can you use for ex. GetComponent?
@LukasLicek thank you for commenting, I have indeed checked if the masterserver.hotslist is properly working and I do not $$anonymous$$d using the get component as long as it works with every single one of the buttons that I have instantiated, if so then it is perfect
The way I have it set up is that the player can create a server so I have the string serverName, I would like it so that if there are 3 people, Joe Sam and Jill that Sam and Jill created a server "hellllllooooo" and "derps" respectively then when Joe opens up the joining menu there will be 2 buttons, one for "hellllllooooo" and one for "derps" with the names written on each button, thank you, $$anonymous$$ingofweirdos
@$$anonymous$$ingofweirdos I will try to come up with solution code :) Btw, I will not do it now, but you want to use Dropdown fields from Text$$anonymous$$eshPro free assets on Assets store. It's really handy :) See you later ;)
Answer by LukasLicek · Jan 08, 2018 at 09:40 AM
Hi @Kingofweirdos I took code from MasterServer.RequestHostList Scripting API and modified it :)
In inspector:
Create empty GameObject and call it for Ex. "ButtonManager".
Create UI.Canvas so you can place UI.
Create Another UI.Canvas and call it "ButtonCanvas", this will be root for your buttons.
Create one button and call it "ServerButtonPrefab". Set it as your first server button, then make prefab of it and delete it.
Place my script on ButtonManager object.
In inspector set Button prefab to recetly created ServerButtonPrefab
Set target Area to ButtonCanvas
Set preferable offset (how far away will be buttons created)
Hit play and let me know if everything is doing alright :)
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[i].gameName); //create button createButton(hostData[i].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-index*offset, newButton.transform.position.z); //set text newButton.GetComponentInChildren<Text>().text = text; } }
PS: I would recommend using dropdown instead of buttons.
@LukasLicek thank you, I didn't understand how we could change the each a value between each of the objects instantiated but this helps a lot thank you. P.S. what is sad is that my code is mostly like yours just missing a few lines
Your answer
Follow this Question
Related Questions
How to access Instantiated GameObject's attached script's values? 1 Answer
Multiple Cars not working 1 Answer
How to make objects spawn at the same time without colliding ? 1 Answer
[SOLVED]: Bullets passing through platforms - Brackeys Tutorial 1 Answer
How to instantiate a sprite after it has been destroyed? 2 Answers