Search Unity

Question change ownership not working

Discussion in 'Multiplayer' started by miccalisto, Apr 23, 2023.

  1. miccalisto

    miccalisto

    Joined:
    Feb 20, 2018
    Posts:
    40
    The ownership change appears to work on the server side but is not properly registered on the clients side.
    In this code the first print and last print shwos the same number 0, the owner.ClientId.Value is 1, so the method ChangeOnwershipServerRpc(owner.ClientId.Value) doesn't change it.
    When using print(OwnerClientId) inside the ChangeOnwershipMethod(ulong clientId) it shows the correct owner. So why the client doesnt catch the change? I read the onwershipchange is autosynchronized.

    Maybe the the information about ownership change couldn't reach from the server to client in time? In that case how do I wait for the information.
    Code (CSharp):
    1. print(OwnerClientId);
    2. print(owner.ClientId.Value);
    3. ChangeOnwershipServerRpc(owner.ClientId.Value);
    4. print(OwnerClientId);
    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false)]
    2.     private void ChangeOnwershipServerRpc(ulong clientId)
    3.     {
    4.         ChangeOnwershipMethod(clientId);
    5.     }
    6.  
    7.     private void ChangeOnwershipMethod(ulong clientId)
    8.     {
    9.         GetComponent<NetworkObject>().ChangeOwnership(clientId);
    10.     }