MirrorNetworking / Mirror

#1 Open Source Unity Networking Library
https://mirror-networking.com
MIT License
5.18k stars 767 forks source link

SyncVars are not always synced on client after OnStartClient #2538

Closed davidbardos closed 3 years ago

davidbardos commented 3 years ago

Describe the bug SyncVars are not always synced when the OnStartClient function gets called. I have a simple bool syncvar with a hook. The value is true on the host while it is false when OnStartClient is called and then few frames later the hook gets called and the bool is true as it is supposed to be.

Expected behavior: "OnStartClient: The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client."

Desktop (please complete the following information):

emrys90 commented 3 years ago

Are you setting it after the NetworkServer.Spawn call? I believe its only guaranteed if you set it before.

davidbardos commented 3 years ago

I set it on the server before the client connects so I guess it should be synced....

My case is the following: 1: Object gets spawned on the server 2: I Set the syncvar to true 3: Client connects 4: On client side the bool is false OnStartClient 5: bool is true a few frames later when the hook gets called.

Honestly I thought the hook wouldn't be called on init, only later if the value changes but it is not a problem at all... the problem is that unless I use hooks everywhere, there is no way to know if a syncvar is updated on client after connection or not.

MrGadget1024 commented 3 years ago

Which OnStartClient are you in? Network Manager's, or a Network Behaviour on the object?

davidbardos commented 3 years ago

Network Behaviour (same object where the syncvar is)

MrGadget1024 commented 3 years ago

Cannot reproduce.

Created new scene with custom NetworkManager from Script Templates with this code:

    public override void OnStartServer()
    {
        NetworkServer.Spawn(Instantiate(spawnPrefabs[0]));
    }

Made cube prefab and added to Registered Spawnable Prefabs list with this NetworkBehaviour:

using UnityEngine;
using Mirror;

public class TestSyncVar : NetworkBehaviour
{
    [SyncVar(hook = nameof(OnValueChanged))]
    public bool test;

    public override void OnStartServer()
    {
        test = true;
    }

    public void OnValueChanged(bool oldValue, bool newValue)
    {
        Debug.Log($"OnValueChanged {test} {oldValue} -> {newValue}");
    }

    public override void OnStartClient()
    {
        Debug.Log($"OnStartClient {test}");
    }

    void Start()
    {
        Debug.Log($"Start {test}");
    }
}

Build and run as server, play client in editor, console output as expected: image

Additionally, I added similar debug logging to the RandomColor script used in the Room Example for the spawned Prize object, and got exactly the same results for all of them.

davidbardos commented 3 years ago

As there was a bug in the latest Mirror that did not allow an android client to connect, I am using 26.2.2 right now so maybe this is the reason I experience it and you don't?

MrGadget1024 commented 3 years ago

As there was a bug in the latest Mirror that did not allow an android client to connect, I am using 26.2.2 right now so maybe this is the reason I experience it and you don't?

Unlikely since no work has been done in this area in quite some time, but the Android issues have been resolved if you want to try the latest build.

If using Kcp Transport, make sure to reset all in-place Kcp Transports already in use.

BlackwispKaan commented 7 months ago

https://forum.unity.com/threads/syncvar-not-working.338655/)](https://forum.unity.com/threads/syncvar-not-working.338655/