Need some help with understanding Mirror.

Hi everyone. I am trying to understand how to work with Mirror. However, I ran into the problem of not being able to synchronize a gameobject between clients. That’s why I made this simple script to figure out what went wrong.

using UnityEngine;
using Mirror;

public class MirrorTest : NetworkBehaviour
{
    [SerializeField] private GameObject mirrorGameObject;
    private void Update()
    {
        if (Input.GetKey(KeyCode.Z))
        {
            mirrorGameObject.SetActive(true);
        }
        if (Input.GetKey(KeyCode.X))
        {
            mirrorGameObject.SetActive(false);
        }
    }
}

It turns out that I know nothing about Mirror at all. I can’t even figure out how to synchronize this simple action.
I know that my request might be stupid, but I just can’t understand it myself.

Most of the time for synchronize you’ll want to use [SyncVar] attribute on a field

I suggest looking at the examples that come with mirror because they will show how to do some of the basic stuff.

There is also more information on synchronization in the mirror docs

1 Like
public GameObject childObj; // local reference of object
// make sure child scripts and gameobjects doesnt have network identity or use network behaviour
// if it needs those, it can use its parent

[SyncVar(hook = nameof(OnChanged))]
public bool objActive = true;

void OnChanged(bool _Old, bool _New)
{
// syncvar objActive is changed before hooks called, you can also use _New.
 childObj.SetActive(objActive);
}


[Command]
public void CmdChangeStatus(bool _value)
{
// you can add cheat checks here, and in the hook
    objActive = _value;
}

private void Update()
{
// so only your player runs the code, can use hasAuthority
 if( isLocalPlayer == false ) return;

        if (Input.GetKey(KeyCode.Z))
        {
            CmdChangeStatus(true);
        }
        if (Input.GetKey(KeyCode.X))
        {
            CmdChangeStatus(false);
        }
}

All this presumes this script is on your player.
We use sync var and hook in this case, as it ‘buffers’ to new joining players, so late joiners get current sync var status, and that calls the hook. Takes a lot of the manual work away, they’re very handy :slight_smile:

Wrote on my phone so havnt tested, good luck!
Edit: added more info

1 Like

@svinoman ^

1 Like

Thank you so much!

It is good to know how to do it the right way. This mess of a code is what I made myself to make it work if anyone is interested:

using UnityEngine;
using Mirror;

public class MirrorTest : NetworkBehaviour
{
    private GameObject mirrorGameObject;
    private void Awake()
    {
        mirrorGameObject = GameObject.Find("MirrorTest");
    }
    private void Update()
    {
        if(isLocalPlayer)
        {
            if (Input.GetKey(KeyCode.Z))
            {
                CmdClientToServerZ();
            }
            if (Input.GetKey(KeyCode.X))
            {
                CmdClientToServerX();
            }
        }
    }

    [Command]
    void CmdClientToServerZ()
    {
        RpcServerToClientZ();
    }
    [ClientRpc]
    void RpcServerToClientZ()
    {
        mirrorGameObject.SetActive(true);
    }
    [Command]
    void CmdClientToServerX()
    {
        RpcServerToClientX();
    }
    [ClientRpc]
    void RpcServerToClientX()
    {
        mirrorGameObject.SetActive(false);
    }
}

Thank guys