Y
Y
Yugg02021-01-20 19:41:14
Unity
Yugg0, 2021-01-20 19:41:14

How to sync data from client to host using Mirror Network in Unity?

When exploring Mirror, I ran into the problem that the host transmits data to clients, and the clients do not transmit data to the server.
I implemented the transfer through "SyncVar":

spoiler
public class MirrorTest : NetworkBehaviour
{
    [SyncVar(hook = nameof(SetColor))]
    public Color playerColor = Color.black;

    public SpriteRenderer playerSprite;
    public Color[] colors;

    private void Update()
    {
        if (isLocalPlayer)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                int randomColor = Random.Range(0, colors.Length);
                playerColor = colors[randomColor];
            }
        }
    }

    void SetColor(Color oldColor, Color newColor)
    {
        playerSprite.color = newColor;
    }
}


Bottom line:
If the host presses "Space" it changes color (in the inspector), and both the host and the client see this.
If the client presses "Space" it changes the color (in the inspector), but neither the client nor the host see the new color.

If you need to clarify something, please ask.
Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question