This is my first time working on a multiplayer game, so I have no experience in this field.
My goal is to make a simple quiz game where there is a game master who can send questions to the candidates. The candidates then answer and the game master can award points. At the moment I am still completely at the beginning.
The tutorials I’ve seen are all about 3D characters and movement, but for me it’s mostly about synchronizing UI elements.
I have a CandidateSpawner, which is stored in the NetworkManager as a player and is automatically spawned for client and host. Then I have a GameManager, in which a Panel Prefab (with the script Candidate) and a Canvas in the Scene are deposited.
The CandidateSpawner, Candidate and the GamesManager are on an Object with Network Object component.
The CandidateSpawner calls a function in the GameManager in which a new panel is instantiated as a child of the Canvas. Currently this should happen on the host as well as on the client. This works correctly for the host, but not for the client. In the client both panels are instantiated, but not as child of the canvas, but simply in the hierarchy, so they are not displayed. In the client I can’t move them because you can only change the parent as a host.
How do I get the panels to be instantiated as a child of the canvas?
CandidateSpawner
public class CandidateSpawner : NetworkBehaviour
{
public override void OnNetworkSpawn()
{
if(IsOwner)
{
SpawnPlayer("new Player");
}
}
private void SpawnPlayer(string name)
{
if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
{
GameManager.instance.SpawnPlayer(name);
}
else
{
GameManager.instance.SpawnPlayerServerRpc(name);
}
}
}
GameManager
public static GameManager instance;
[SerializeField] private GameObject candidateCanvas;
[SerializeField] private GameObject candidtaePanel;
private void Awake()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(this);
}
else
{
Destroy(this);
}
}
[ServerRpc(RequireOwnership = false)]
public void SpawnPlayerServerRpc(string name)
{
Candidate newCandidate = Instantiate(candidtaePanel, candidateCanvas.transform).GetComponent<Candidate>();
newCandidate.GetComponent<NetworkObject>().Spawn();
newCandidate.candidateName = name;
}
public void SpawnPlayer(string name)
{
Candidate newCandidate = Instantiate(candidtaePanel, candidateCanvas.transform).GetComponent<Candidate>();
newCandidate.GetComponent<NetworkObject>().Spawn();
newCandidate.candidateName = name;
}