Skip to content

OwnedObjects of clientId is not set correctly after ChangeOwnership(clientId) #1525

Closed
@phisn

Description

@phisn

Describe the bug
If the ownership of an object is changed with ChangeOwnership(clientId), the object is not added to the list of owned objects of this new owner.

Fix
In the current implementation the removal and add is handled in the following way in Runtime/Spawning/NetworkSpawnManager.cs lines 232 to 245.

            if (TryGetNetworkClient(networkObject.OwnerClientId, out NetworkClient networkClient))
            {
                for (int i = networkClient.OwnedObjects.Count - 1; i >= 0; i--)
                {
                    if (networkClient.OwnedObjects[i] == networkObject)
                    {
                        networkClient.OwnedObjects.RemoveAt(i);
                    }
                }

                networkClient.OwnedObjects.Add(networkObject);
            }

            networkObject.OwnerClientId = clientId;

Here the object is added to the old owner, instead of the new owner. This bug is fixed by changing the code to the following.

            if (TryGetNetworkClient(networkObject.OwnerClientId, out NetworkClient networkClient))
            {
                for (int i = networkClient.OwnedObjects.Count - 1; i >= 0; i--)
                {
                    if (networkClient.OwnedObjects[i] == networkObject)
                    {
                        networkClient.OwnedObjects.RemoveAt(i);
                    }
                }
            }

            networkObject.OwnerClientId = clientId;
            
            if (TryGetNetworkClient(networkObject.OwnerClientId, out NetworkClient newNetworkClient))
            {
                newNetworkClient.OwnedObjects.Add(networkObject);
            }

So the object is added to the actual new owner of the object.

Metadata

Metadata

Assignees

Labels

priority:mediumThis issue has medium priority and may take some time to be resolvedtype:bugBug Report

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions