When SyncVar changes, it creates a new var insted of changing the old one…

I am working on a Multiplayer Game in Unity, and am using the mirror to handle the multiplayer. I need to store some user input data into variables and I want these to be Synced between the users.

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

public class PlayerManager : NetworkBehaviour
{
    [SyncVar]
    public int setDiceRoll;

    [SyncVar]
    public int setDiceAmount;

    public int GetSetDiceRoll()
    {
        return setDiceRoll;
    }

    public int GetSetDiceAmount()
    {
        return setDiceAmount;
    }

    [Command]
    public void CmdModifySetDiceRoll(int newValue)
    {
        setDiceRoll = newValue;
    }

    [Command]
    public void CmdModifySetDiceAmount(int newValue)
    {
        setDiceAmount = newValue;
    }

    private int setDiceRollNew;
    private int setDiceAmountNew;

    private void Start() 
    {
        diceText.text = setDiceRoll.ToString();
        amountText.text = setDiceAmount.ToString();
    }

    public void Update()
    {
        Debug.Log(setDiceAmount.ToString() + " " + setDiceRoll.ToString());
    }

The setDiceAmountNew and setDiceRollNew are inputted here by the user through UI. 
Then, by clicking on a button, the MoveToNext method is called. 

    public void MoveToNext()
    {
        if(setDiceAmountNew > setDiceAmount || setDiceRollNew > setDiceRoll)
        {
            CmdModifySetDiceAmount(setDiceAmountNew);
            CmdModifySetDiceRoll(setDiceRollNew);
        } 
    }

    public void Refresh()
    {
        Debug.Log(GetSetDiceRoll());
        Debug.Log(GetSetDiceAmount());

        diceText.text = setDiceRoll.ToString();
        amountText.text = setDiceAmount.ToString();
    }
}

The variables setDiceRoll and setDiceAmount are the ones in question. They are initially set to 1 and 1 in an OnServerAddPlayer, when the first player joins the server.

The Player can then change the value of setDiceRollNew and setDiceAmountNew by hitting + and - buttons, and then hit Go, to call the MoveToNext() method. This method calls the two commands that should update the setDiceRoll and setDiceAmount SyncVars.

This works with one player. I can change the values according to what the user inputs, however once another player joins, their initial setDiceRoll and setDiceAmount are also set to one and “another pair of SyncVars” is added. This is reflected by the picture below. Shown is the Debug.Log from the Update() method, and you can see the two logs coming at the same time from the two pairs.

[

]https://i.stack.imgur.com/OA10x.png

I have added debug calls to my OnServerAddPlayer method, to check that the second player doesn’t get his values set to 1 1 there and it indeed does not.

I have no clue what is wrong. To my knowledge, the SyncVars are being changed by the server (Commands) are being used.

What am I missing?

I don’t know which pieces of code I should provide. If you want to see the OnServerAddMethod, I will gladly provide it. If you want to see anything else, or if I should put the project into a repo for you to be able to clone it, I will gladly do that as well. Thanks for any help. It’s probably something stupidly simple, that I’m missing…

Hey Daniel, do you still need help with this? Let me know and I will take a look.

Hi. Sorry, no. I figured it out. I had the sync var creation done in the player manager script so every time a player was spawned, a new sync var had been created and that was the problem…

How would I go about either deleting or closing this thread ?

1 Like

Privacy & Terms