com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

Server lost ownership but OnGainedOwnership was triggered

Open gooddereklu opened this issue 3 years ago • 1 comments

Description

Hi! guys! I just upgrade to Netcode preview 9, and found a strange behaviour.

I had a network behaviour object that will switch ownership from server to client.

But the network behaviour on the server side will trigger OnGainedOwnership even when it lost owner from server.

This happen on Netcode preview 9 only.

I was using Netcode preview 5 before upgrading.

Reproduce Steps

1.Create Empty Unity Project 2.Donwload Netcode preview 9 package 3.Create a Script and inherit from Network Behaviour 4.Create a method with attribute like this : [ServerRpc (RequireOnwership = false)] 5.Within method from step4, I switch it's ownership by typing "GetComponent<NetworkObject>().ChangeOwnership( clientID )" 6.Create a override OnGainedOwnership method

Actual Outcome

Server side trigger OnOwnershipLost at first and OnGainedOwnership was trigger right after that. Client side was fine.

Expected Outcome

After transport ownership from server to client, the client side should receive OnGainedOwnership, and server should only trigger OnLostOwnership

Screenshots

Environment

  • OS: Mac
  • Unity Version: 2021.3.1
  • Netcode Version: 1.0.0-pre.9

gooddereklu avatar Jun 20 '22 14:06 gooddereklu

Backlog MTT-4081

ashwinimurt avatar Jul 12 '22 23:07 ashwinimurt

This is by design. The server will always receive OnGainedOwnership notifications as it is useful to track (server-side) who has gained ownership when it happens. If you have client-side specific code that needs to only be executed by clients, the best way to handle that would be:

        public override void OnGainedOwnership()
        {
            if (IsServer)
            {
                return;
            }
            // Handle client-side gained ownership 

            base.OnGainedOwnership();
        }

NoelStephensUnity avatar Sep 30 '22 00:09 NoelStephensUnity