Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to spawn a client's character on the server?

Discussion in 'Multiplayer' started by CarterG81, Jul 2, 2015.

  1. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    In the Unity Editor, everything works perfectly fine. The character spawns correctly, the customization of the player works (different Sprite depending on character selection buttons AFTER connection but BEFORE adding the playerPrefab.)

    However when building the game, nothing works. Not hosting, not joining as client. It freezes the game the moment I choose a character (call the function SelectCharacterButton, specifically it freezes whe it calls [Command]Cmd_ClientSpawnCharacter) and that's that. No errors in the Unity Editor version. Cannot test client in the Editor unless as Host (and it works fine).

    I figure I am just doing all of this wrong, so I figure I'd just ask what I'm doing wrong.

    Here is what I've got so far. I've commented out some functions that I've tried or that don't do anything important.



    Code (csharp):
    1.  
    2. //MultiplayerCommands.cs
    3. [Command]
    4.   public void Cmd_ClientSpawnCharacter(PlayerCharacterData playerData, short playerControllerId)
    5.   {
    6.        //ClientScene.Ready(GameNetworkManager.singleton.client.connection);
    7.        ClientScene.AddPlayer(GameNetworkManager.singleton.client.connection, playerControllerId);
    8.       //ClientScene.AddPlayer(playerControllerId); //What I originally did.
    9.   }
    10.  
    Code (csharp):
    1.  
    2. //My UI Buttons select characters using this function.
    3.   public void SelectCharacterButton(string name)
    4.   {
    5.   //LoadCharacterData(name); //Load all the PlayerData for that character. (ex. Name, Stats, Special Starter Items, etc.)
    6.  
    7.   //SpawnPlayer on Server
    8.   multiplayerClient.GetComponent<MultiplayerCommands>().Cmd_ClientSpawnCharacter(multiplayerClient.GetComponent<MultiplayerClient>().playerData, 0);
    9.  
    10.   //Delete this Character Select GUI
    11.   Destroy(this.gameObject);
    12.   }
    13.  

    Code (csharp):
    1.  
    2.   public override void OnStartClient(NetworkClient client)
    3.   {
    4.   Debug.Log("Starting Client...");
    5.   //networkManager.saveSlotNumber = slotNumber;
    6.   //networkManager.isNewGame = NewGame[slotNumber];
    7.  
    8.   if (!GameObject.Find(ClientObject.name)) //Create the Client master object
    9.   {
    10.   thisClientObject = (GameObject)Instantiate(ClientObject, Vector3.zero, Quaternion.identity);
    11.   thisClientObject.name = ClientObject.name;
    12.   DontDestroyOnLoad(thisClientObject);
    13.   }
    14.  
    15.   if (isNewGame)
    16.   {
    17.   if (!GameObject.Find(CharacterSelectGUI.name)) //Create the Character Select GUI to make a new Character.
    18.   {
    19.   thisCharacterSelectGUI = (GameObject)Instantiate(CharacterSelectGUI, Vector3.zero, Quaternion.identity);
    20.   thisCharacterSelectGUI.name = CharacterSelectGUI.name;
    21.   DontDestroyOnLoad(thisCharacterSelectGUI);
    22.   }
    23.   }
    24.   else
    25.   {
    26.   //thisClientObject.GetComponent<MultiplayerServer>().LoadGame(saveSlotNumber);
    27.   }
    28.  
    29.   base.OnStartClient(client);
    30.   Debug.Log("I am Client.");
    31.   }
    32.  


    The entire idea is to do the following:

    1) Connect to Server as Client or StartHost. [Solved]
    2) Do not autospawn playerPrefab. [Solved]
    3) Instantiate the "Client" prefab. This holds various scripts. [Solved]
    4) Instantiate the Character Selection GUI. Click button1 to be "Character1". Click button2 to be "Character2". [Solved]
    5) Based on which button was selected, spawn the playerPrefab which gets the choice (stored in the "Client" prefab) and automatically sets the playerPrefab's sprite to Character1 or Character2. (This is identical to spawning a player with a color choice, or with a string name, or any variable that the player chooses after connecting but prior to spawning the playerPrefab.) [Solved in UnityEditor, Freezes in Build]
     
    Last edited: Jul 2, 2015
  2. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    I got rid of the [Command] and replaced it with a simple ClientScene.AddPlayer(0);

    Code (csharp):
    1.  
    2.   public void SelectCharacterButton(string name)
    3.   {
    4.   //LoadCharacterData(name); //Load all the PlayerData for that character. (ex. Name, Stats, Special Starter Items, etc.)
    5.  
    6.   //SpawnPlayer on Server
    7.   ClientScene.AddPlayer(0);
    8.  
    9.   //Delete this GUI
    10.   Destroy(this.gameObject);
    11.   }
    12.  
    However, when I created a Host and then a Client Connects- the Host sees both characters (self and client's character) while the Client only sees itself (self; client's character. Does NOT see the Host's character.)
     
  3. Deleted User

    Deleted User

    Guest