Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't get GameObjects to display on client upon connecting to server

Discussion in 'Multiplayer' started by Temseii, Apr 9, 2018.

  1. Temseii

    Temseii

    Joined:
    Aug 23, 2016
    Posts:
    14
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're using NetworkServer.SpawnWithClientAuthority incorrectly. You're instantiating and calling that spawn method in a clientrpc, so doing so on every connected client. You can only spawn a networked object on the server though. You should redesign your code so all of the instantiate/spawn calls only happen on the server.
     
  3. Temseii

    Temseii

    Joined:
    Aug 23, 2016
    Posts:
    14
    Thanks for the reply!

    Alright, I've been changing things around a lot and it some things are quite out of place as I'm desperate and don't really know what I'm doing. I moved all the instantiate/spawn calls to to the Command, but now I don't seem to be able to spawn objects at all on the client. Works fine for the host still.

    Here's the new function:

    Code (CSharp):
    1. void CmdHospitalizePlayer() {  
    2.         HospitalPanel = GameObject.Find("HospitalizedPanel");
    3.         PlayerNameText.text = PlayerName;
    4.         HospitalizedPlayer = Instantiate(HospitalizedPlayerPrefab);
    5.         HospitalizedPlayer.name = PlayerName;
    6.         if (!(GameObject.Find("/Canvas/LeftPanel/HospitalPanel/HospitalizedPanel/" + HospitalizedPlayer.name))) {
    7.             HospitalizedPlayer.transform.SetParent(HospitalizedPanel);
    8.         } else {
    9.             Debug.Log(HospitalizedPlayer.name + " is already hospitalized.");
    10.         }
    11.         NetworkServer.SpawnWithClientAuthority(HospitalizedPlayer, connectionToClient);
    12.     }
    I tried removing the NetworkServer Spawn completely but it didn't change anything.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What are you doing with the parenting there? That parenting won't be part of what the server sends to all the clients when it spawns the object. Also it is unsupported to have a NetworkIdentity on a child object, only on the root gameobject.
     
  5. Temseii

    Temseii

    Joined:
    Aug 23, 2016
    Posts:
    14
    Code (CSharp):
    1. [Command]
    2.     void CmdHospitalizePlayer() {  
    3.         HospitalizedPlayer = Instantiate(HospitalizedPlayerPrefab);
    4.         HospitalizedPlayer.name = PlayerName;
    5.         if (!(GameObject.Find("/Canvas/LeftPanel/HospitalPanel/HospitalizedPanel/" + HospitalizedPlayer.name))) {
    6.             RpcHospitalizePlayer();
    7.         } else {
    8.             Debug.Log(HospitalizedPlayer.name + " is already hospitalized.");
    9.         }
    10.     }
    11.  
    12.     [ClientRpc]
    13.     void RpcHospitalizePlayer() {
    14.         HospitalPanel = GameObject.Find("HospitalizedPanel");
    15.         PlayerNameText.text = PlayerName;
    16.  
    17.         HospitalizedPlayer.name = PlayerName;
    18.  
    19.         HospitalizedPlayer.transform.SetParent(HospitalPanel.transform);
    20.     }
    Tried it this way now. The prefabs still get set on the HospitalPanel only on the host. Sorry for being difficult, still trying to wrap my head around all this multiplayer stuff.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That's not going to work either. HospitalizedPlayer is Instantiated on the server, but not even spawned anymore. You're also trying to use a reference to it on the client when the client isn't the one that established the reference. That type of stuff is better placed in the Start method of a script on the object itself.
     
  7. Temseii

    Temseii

    Joined:
    Aug 23, 2016
    Posts:
    14
    Would you mind giving an example of how it should look like?
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't exactly understand what you're trying to do, so I can't really. What I would do is instantiate/spawn your object from a prefab on the server. Whatever you need to do with it in relation to other scene objects on the clients when it spawns on them I'd do in a Start method. I would not set another object to be the spawned object's parent. Leave the object in the root of the scene hierarchy.

    If this is a UI object, I wouldn't do it this way at all. Instead I'd just create the UI object locally and send information back and forth between the clients and server via something like Unet Messages. The Unet instantiate/spawn stuff isn't really made for use with objects in a UI hierarchy.
     
  9. Temseii

    Temseii

    Joined:
    Aug 23, 2016
    Posts:
    14
    It is indeed all UI. I'll give those Messages a try.
     
  10. Temseii

    Temseii

    Joined:
    Aug 23, 2016
    Posts:
    14
    Could you possibly come up with an example build of how it should look like? I've banged my head against this for long it's starting to be really frustrating to see no progress.