Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Data wont send through NetworkConnection.Send using Mirror

Discussion in 'Multiplayer' started by markahughes88, Sep 4, 2020.

  1. markahughes88

    markahughes88

    Joined:
    Aug 24, 2020
    Posts:
    19
    I've decided to swap from Photon PUN2 to Mirror.

    I'm working on player spawns and I'm trying to change the gameobject based on the clients player stats that I have saved in a class.

    I've read the documentation and followed what it says to do, created a "message" to send to the server about the player as they spawn and then populated that with the relevant data from the PlayerDataClass.

    Inside of the `OnClientConnect` function this all works fine and the "message" is populated with the correct data. However when I try and send that data to my `OnCreatePlayer` function which handles spawning the player and customising it, the "message" data is empty.

    The message is sent to the `OnClientConnect` function through a `NetworkConnection.Send()`.

    You can see in my code two sets of debug.logs. The first gives the correct readings, the 2nd is empty.

    I can't work out why and I've been banging my head against the wall with this all day. Any help would be greatly appreciated.

    Code (CSharp):
    1. using Mirror;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using TMPro;
    7.  
    8. public class NetworkManager : NetworkManager
    9. {
    10.     // Scripts
    11.     private PlayerData pd;
    12.  
    13.     public class PlayerStyles : MessageBase
    14.     {
    15.         public string username;
    16.         public string color1;
    17.         public string color2;
    18.         public string pattern;
    19.         public string outfit;
    20.     }
    21.  
    22.     public override void OnStartServer()
    23.     {
    24.         base.OnStartServer();
    25.  
    26.         NetworkServer.RegisterHandler<PlayerStyles>(OnCreatePlayer);
    27.     }
    28.  
    29.     public override void OnClientConnect(NetworkConnection conn)
    30.     {
    31.         base.OnClientConnect(conn);
    32.  
    33.         // Import scripts
    34.         pd = gameObject.GetComponent<PlayerData>();
    35.  
    36.         PlayerStyles playerStyles = new PlayerStyles
    37.         {
    38.             username = pd.m_Player.Username,
    39.             color1 = pd.m_Player.Color1,
    40.             color2 = pd.m_Player.Color2,
    41.             pattern = pd.m_Player.Pattern,
    42.             outfit = pd.m_Player.Outfit
    43.         };
    44.  
    45.         Debug.Log("styles are " + playerStyles);
    46.         Debug.Log("username " + playerStyles.username);
    47.         Debug.Log("color1 " + playerStyles.color1);
    48.         Debug.Log("color2 " + playerStyles.color2);
    49.         Debug.Log("pattern " + playerStyles.pattern);
    50.         Debug.Log("outfit " + playerStyles.outfit);
    51.  
    52.         conn.Send(playerStyles);
    53.     }
    54.  
    55.     void OnCreatePlayer(NetworkConnection conn, PlayerStyles styles)
    56.     {
    57.         // playerPrefab is the one assigned in the inspector in Network Manager
    58.         GameObject go = Instantiate(playerPrefab);
    59.  
    60.         Debug.Log("styles are " + styles);
    61.         Debug.Log("username " + styles.username);
    62.         Debug.Log("color1 " + styles.color1);
    63.         Debug.Log("color2 " + styles.color2);
    64.         Debug.Log("pattern " + styles.pattern);
    65.         Debug.Log("outfit " + styles.outfit);
    66.  
    67.         // Call this to use this gameobject as the primary controller
    68.         NetworkServer.AddPlayerForConnection(conn, go);
    69.     }
    70. }
     
    MilenaRocha likes this.