Search Unity

Bug OwnedObjects for LocalClient seems always to be 0 (because not implemented?)

Discussion in 'Netcode for GameObjects' started by ManuelRauber, Feb 24, 2022.

  1. ManuelRauber


    Apr 3, 2015

    I'm currently fiddling around with NetCode for GameObjects version 1.0.0-pre.5 and I've set up the following scenario:

    I have a SpawnManager that creates the player objects like this:

    Code (CSharp):
    1. var playerObject = Instantiate(PlayerPrefab);
    2. = $"Player {clientId.ToString()}";
    3. playerObject.SpawnAsPlayerObject(clientId, true);
    Then, within the SpawnShip method of the PlayerController I do the following to spawn in the player's visual representation:

    Code (CSharp):
    1. public void SpawnShip(Vector3 position, Quaternion rotation)
    2. {
    3.   PDebug.Log("Spawning ship for player {0}", OwnerClientId);
    5.   var networkShip = Instantiate(ShipPrefab, position, rotation);
    6. = $"{name}_Ship";
    7.   networkShip.SpawnWithOwnership(OwnerClientId, true);
    8. }
    As you can see, I spawn the object with SpawnWithOwnership and I put in the OwnerClientId. Via the log I can verify that the server outputs the correct Id, so 0 for player 1 and 1 for player 2.

    However, I also have this little debug UI that prints various things to the screen

    Code (CSharp):
    1. GUILayout.Label("Owned Objects:" + NetworkManager.Singleton.LocalClient?.OwnedObjects?.Count);
    The host outputs that it has 1 owned object, which is the additional spawned ship.
    However on another client (with OwnerId 1) it reports it does not have owned objects hence being spawned with SpawnWithOwnership and id 1. I also tried using ChangeOwnership immediately, but it does not help.

    Am I missing here something or do I have a misunderstanding about OwnedObjects?

    I also dug a bit into the code, and I've found this here:

    (see picture 1)

    This is located in class NetworkSpawnManager. As I can see here, that on server side, OwnedObjects are tracked correctly. But on client side, this piece of code is missing.

    I then went on to see the message handling of ChangeOwnership, that looks like:

    (see picture 2)

    on client side, the debugger did enter Line 35 and 43, so the owner is set correctly, so it only looks like the tracking of the OwnedObjects is missing on client side.

    Or is that intended, that the client does not track ownership of NetworkObjects? If so, the API is misleading, because NetworkManager.Singleton.LocalClient has a property OwnedObjects.


    Edit: For whatever reason I get an access denied error, if I try to add the pictures directly into the text as full image or thumbnails via the forum buttons. :(

    Attached Files:

  2. reitenator


    Sep 25, 2019
    I have a very similar problem myself.

    I started with using only the Start/Awake method to find out which are the current client's objects, but then I thought maybe the client needs some more time to initialize itself, or something.

    I ended up by putting the following in the update method.

    When checking the HOST, it finds its own objects, but for the other CLIENTs the count is always zero, even though I can "physically" see in the Hierarchy that there are objects in the scene which are supposedly owned by all of the connected clients.

    Code (CSharp):
    2. if (NetworkManager.Singleton.LocalClient.OwnedObjects.Count == 0) return;
    3.         if (!ownedObjectsSet) {
    4.             ownedObjectsSet = true;
    5.             Debug.Log($"There are about {NetworkManager.Singleton.LocalClient.OwnedObjects.Count} owned objects on My Client...");
    6.             foreach (var myObject in NetworkManager.Singleton.LocalClient.OwnedObjects) {
    7.                 Debug.Log($"ThisObject is: {myObject}");
    8.                 if (myObject.GetComponent<NetworkAimTarget>() == true) {
    9.                     aimTarget = myObject;
    10.                     Debug.Log("found object in scene.....");
    11.                 }
    13.             }
    14.         }
  3. luke-unity


    Sep 30, 2020
    This looks like a bug from other side. Could you report a bug using the Unity bug reporter so that we can look into fixing this?
  4. ManuelRauber


    Apr 3, 2015
    Hi Luke,

    I've just created a new bug report. Case number: 1410393

    Thanks for taking a look into it!
    luke-unity likes this.
  5. ManuelRauber


    Apr 3, 2015
    codeBatt and luke-unity like this.