Mirror [Sync Var] not working

So I tried following along to lecture 5 Syncing variables but when I build my app and start the Host/client on the built application and then then run the Unity Editor as the client my displayname still says “Missing name” for both cloned versions of the player in the inspector window. Any help would be appreciated please.

MyNetworkPlayer code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class MyNetworkPlayer : NetworkBehaviour
{
    [SyncVar]
    [SerializeField] 
    private string displayName = "Missing Name";

    [Server]
    public void SetDisplayName(string newName)
    {
        displayName = newName;
    }
}

MyNetworkManager code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class MyNetworkManager : NetworkManager
{
    public override void OnClientConnect(NetworkConnection conn)
    {
        base.OnClientConnect(conn);

        Debug.Log("I connected to a server");
    }

    public override void OnServerAddPlayer(NetworkConnection conn)
    {
        base.OnServerAddPlayer(conn);

        MyNetworkPlayer player = conn.identity.GetComponent<MyNetworkPlayer>();
        player.SetDisplayName($"Player {numPlayers}");
    }

}

So I finally redid my project and the code above works. Some things I did differently this time where

  • Used Unity 2019.4.15f1 instead of Unity 2020.2
  • Used Mono as the compiler and Not IL2CPP
  • So mirror was already in my assets and last time I just imported it through package manager. I didn’t notice the update button next to import and this time I updated the version saved in my assets and then imported into a fresh project. This is probably what the problem was

Hi John, do you still need help with this problem?

No thank you I figured it out thanks!

Privacy & Terms