- Home /
How would you spawn UI GameObjects inside a grid in multiplayer?
I've made a panel with a grid layout to which I'm instantiating gameobjects. I've succeeded in that I'm able to instantiate objects with the panel as their parent and the gameobjects are displayed for all connected players. But it has come with its problems. The biggest one being that I haven't been able to load the instantiated gameobjects (which are prefabs) for players that connect late.
How would you go around designing something like this? I've been fighting with these for days and just don't understand the networking well enough to come up with a solid solution.
Answer by Temseii · Apr 11, 2018 at 08:26 PM
Hey there, in case you were interested in how the situation turned out, I got help from a redditor and finally managed got the problem fixed. :)
https://www.reddit.com/r/Unity3D/comments/8bifmp/loading_spawned_prefabs_upon_connecting_to_server/
Answer by seandolan · Apr 10, 2018 at 01:18 PM
You probably just need to check all the child objects of the parent when the player connects. It depends on what networking solution you are using but most have an OnConnect or similar function. In there, cycle through the children of the parent and detect if they need to be placed. Comment on here if you don't know how to get a list of children of a parent and I will post you some code.
Hey, thanks for the reply!
I tried doing that like this;
[ClientRpc]
void RpcLoadHospitalizedPlayers() {
HospitalizedPanel = GameObject.Find("HospitalizedPanel").transform;
int children = HospitalizedPanel.childCount;
for (int i = 0; i < children; i++) {
GameObject toSpawn = HospitalizedPanel.GetChild(i).gameObject;
Debug.Log("Found child: " + HospitalizedPanel.GetChild(i));
if (hasAuthority) {
GameObject HospitalizedPlayer = Instantiate(toSpawn);
HospitalizedPlayer.transform.SetParent(HospitalizedPanel);
} else
Debug.Log("No authority.");
}
}
But the gameobjects in the scene would only get cloned for the host and not the clients. I tried editing things around a lot and calling things through Commands ins$$anonymous$$d of ClientRpc but haven't been able to get it functioning properly.
Yep, that's exactly what I was thinking would work. Not sure why it isn't as I am not 100% on the code myself for that stuff. I do believe (even though it's not worded the same) that this question on here has the information you need. One solution in the comments section was to store the data in a different form and send it through the server to the clients. I am happy to help you research more into this if I can but sending you this link is the next step I can recommend. Hopefully it helps and it didn't show up on your radar because they are phrasing things a little differently. Best of luck mate, how this gets you where you need to be:
https://answers.unity.com/questions/1140982/how-to-properly-use-clientrpc-and-command.html
How would you go around storing the data? All the server/client stuff sounds so simple when hearing people talk about it but my brain hits a total break when it comes to actually implementing it haha...
I don't know the specific method @nyaw was using. Perhaps try breaking the steps down to specific questions and we might get some more input here from others. Like "How to send data to ClientRCP through Command in Server". I wish I knew the answer straight up for you buddy. I think this learning is important for you to go through now at least because if you just had to copy and paste some code to fix it.. Imagine how hard it would be to diagnose problems down the tracking without understanding it's inner workings.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Loading spawned prefabs upon connecting to server? 1 Answer
Creating interactable enviornment items that are usable by all players in Multiplayer 2 Answers
Can players host a server individually on your game for free 1 Answer
send data from client to local server 0 Answers