Skip to content

OnValueChanged is not called when Ownership changes also occur #2745

Open
@zachstronaut

Description

@zachstronaut

When a (non-host) client calls a Server RPC that both changes Ownership and changes the value of a NetworkVariable, that non-host client will not receive a OnValueChanged event.

public class Test : NetworkBehaviour
{
    public NetworkVariable<int> test = new NetworkVariable<int>(-1, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);

    [ServerRpc(RequireOwnership = false)]
    public void InitializeAndClaimOwnership_ServerRpc(int _test, ServerRpcParams _rpcParams = default)
    {
        NetworkObject.ChangeOwnership(NetworkManager.ServerClientId);
        test.Value = _test;
        NetworkObject.ChangeOwnership(_rpcParams.Receive.SenderClientId); // Remove this line, and the non-host client will receive the OnValueChanged event for the test NetworkVariable
    }
}

public class Observer : MonoBehaviour
{
    void SomeClientMethod(Test test)
    {
        test.test.OnValueChanged += (previousValue, newValue) =>
        {
            // Will not fire on non-host client
            Debug.Log($"Previous value: {previousValue}, new value: {newValue}");
        };

        test.InitializeAndClaimOwnership_ServerRpc(0);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:awaiting-responseAwaiting response from author. This label should be added manually.type:bugBug Report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions