Search Unity

IsOwner is not set to true when spawning object with ownership!

Discussion in 'Netcode for GameObjects' started by galactichyperstar, Jan 12, 2022.

  1. galactichyperstar

    galactichyperstar

    Joined:
    Jun 4, 2018
    Posts:
    11
    I am spawning an object with "SpawnWithOwnership" but on the client-side, the "OnNetworkStart" method has the "IsOwner" set to false. When I look at the object in the Unity editor, the object has the "IsOwner" set to true.

    I'm assuming during the "OnNetworkStart" the "IsOwner" is not set? If it is not set in "OnNetworkStart" then when is it set?
     
    maxkcy likes this.
  2. galactichyperstar

    galactichyperstar

    Joined:
    Jun 4, 2018
    Posts:
    11
    I just tested this by using a coroutine and waiting until the end of the frame to check for "IsOwner" and it is true.
    Meaning, for some reason, "IsOwner" is not set during "OnNetworkStart" when an object is spawned with ownership, so in the "OnNetworkStart" I have to start a coroutine to wait until the end of the frame and check the "IsOwner" there because by then it seems to be set.
     
    maxkcy likes this.
  3. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Which version of netcode for gameobjects are you using?
     
  4. galactichyperstar

    galactichyperstar

    Joined:
    Jun 4, 2018
    Posts:
    11
  5. PinkTsunami

    PinkTsunami

    Joined:
    Dec 8, 2014
    Posts:
    3
    I'm suffering from same issue in pre.6
     
  6. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
  7. UnkelRambo

    UnkelRambo

    Joined:
    Oct 26, 2012
    Posts:
    80
    Any update on this? I updated to Netcode v1.0.1 and IsOwner is false for my player objects during OnNetworkSpawn(). I've got a workaround for the few instances that I currently use this, but it's error prone and adds some complexity.

    I've looked around for something I might be doing wrong but I'm not seeing it. Definitely not ruling it out though ;)
     
  8. CreativeChris

    CreativeChris

    Unity Technologies

    Joined:
    Jun 7, 2010
    Posts:
    457
    Could you submit a GitHub issue?
     
  9. UnkelRambo

    UnkelRambo

    Joined:
    Oct 26, 2012
    Posts:
    80
    Done. I haven't tested this small scenario out of project so hopefully it actually does repro lol.
     
  10. Gazzzah

    Gazzzah

    Joined:
    Jul 18, 2018
    Posts:
    16
    I'm having the same issue. How did your workaround work?
    I'm considering just using (OwnerClientID == NetworkManager.Singleton.LocalClientId) at this point
     
    grunch1, maxkcy and lelmota like this.
  11. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    I am seeing this too. I see that the NetworkBehaviour IsOwner is false, but the NetworkObject.IsOwner is true.

    Version 1.1.0 - October 20, 2022
    Unity 2021.3.1f1

    It seems the network spawning does not like to work when spawning another object from another.
    I use the NetworkManager to spawn my local player. In that script I then spawn the actual player object.
    Code (CSharp):
    1.     public override void OnNetworkSpawn()
    2.     {
    3.         base.OnNetworkSpawn();
    4.  
    5.         if (IsServer)
    6.         {
    7.             var local_player = GameObject.Instantiate(_PlayerPrefab);
    8.             local_player.name = string.Format("Player {0}", OwnerClientId);
    9.             _SpawnedPlayerNetwork = local_player.GetComponent<PlayerNetwork>();
    10.             _SpawnedPlayerNetwork.gameObject.SetActive(true);
    11.             _SpawnedPlayerNetwork.NetworkObject.SpawnWithOwnership(OwnerClientId, true);
    12.         }
    13.     }
    14.  
    This fails, the client object is spawned but with a warning about the object id already exists. The ownership is wrong as well.

    A workaround I start a coroutine instead and wait a couple of frames. Then I spawn the network objects. This seems to work correctly.

    Not sure why the first case isn't working. I see this being used around in the documentation. And didnt see any restrictions on spawning new objects during OnNetworkSpawn.
     
  12. Hardstop

    Hardstop

    Joined:
    Aug 31, 2013
    Posts:
    23
    Same issue here, when using the networkmanager script to spawn the player, it spawns the player object for the client without ownership.
     
  13. UnkelRambo

    UnkelRambo

    Joined:
    Oct 26, 2012
    Posts:
    80
    I think my GitHub issue on this was closed as NO REPRO, my repro case wasn't up to par :D

    My issue seemed to magically resolve itself so I'm not sure what the heck was happening, but I DID update to Netcode for GameObjects v1.0.2 so /shrug
     
  14. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    I asked this internally and will reply here as soon as I have an answer (keep in mind that most of Unity is on vacation right now, so it might take some time)
     
  15. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    259
    If anyone is still having issues with this please provide additional details with script(s) and provide a description regarding the context of the NetworkObjects being spawned =or= if you can provide a small project repo link that will expedite the process and most likely get a more precise response.

    @daxiongmao:
    I am assuming that the NetworkBehaviour code provided is attached to a player network prefab?
    If so, is this assigned to the NetworkManager's player prefab or spawned via the connection approval process?
    If not, could you provide some additional details on what the context of the script is?

    From the script code segment provided above, there is one line that could potentially be problematic:
    Code (CSharp):
    1. _SpawnedPlayerNetwork.gameObject.SetActive(true);
    Is the _SpawnedPlayerNetwork GameObject in-scene placed?

    If it is an in-scene placed NetworkObject and you are running a Host (that uses the same script when spawning its local player) then that would most likely be the issue.

    If not, then could you describe what _SpawnedPlayerNetwork is and why you need to set it to active in the hierarchy?

    Once I have a bit more information I will be better able to assist you with this issue (if you are still experiencing it).
     
  16. Archimagus

    Archimagus

    Joined:
    Sep 29, 2012
    Posts:
    21
    I have a small repo here that demostrates the problem. My NetworkPlayer spawns fine with owner permission. But as the server when I try and have that player spawn a ship for himself with ownership, the ownership is never given to the client and so he can't control the ship. Spawning happens in NetworkPlayer.cs line 57.
    Archimagus/NetcodeForGameObjectsTest (github.com)
     
  17. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @Archimagus , thanks for sharing the code. Would it be possible for you to open a bug report here? In this way, the team behind Netcode For GameObject will be able to prioritize your fix, and you'll be automatically updated about its status.
     
    tobinami likes this.
  18. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    In my setup that code was on the Player Spawner Prefab.
    upload_2023-1-24_15-1-51.png
    So when the system automatically spawned the new player. It would then spawn another object dynamically not in scene placed.
    _SpawnedPlayerNetwork is just a reference in the script holding onto that new object. To avoid constantly having to get component calls. It is created by the couple lines above instantiating the _PlayerPrefab.

    My reasoning for this logic was I want to be able to spawn different prefabs based on team or class chosen.
    And it seamed simpler to do there than trying to get the auto spawning stuff to make the selection.

    The good news is that after updating to 1.2.0 It works as expected and I could remove the coroutine and delays I was using.

    And this is the code i use now basically is what I posted originally.

    Code (CSharp):
    1.  
    2.     [SerializeField]
    3.     private PlayerNetwork _SpawnedPlayerNetwork;
    4.  
    5.     private void SpawnPlayer()
    6.     {
    7.         var local_player = GameObject.Instantiate(_Team.Value == 0 ? _PlayerPrefabTeam1 : _PlayerPrefabTeam2);
    8.         local_player.name = string.Format("Player {0}", OwnerClientId);
    9.         _SpawnedPlayerNetwork = local_player.GetComponent<PlayerNetwork>();
    10.         _SpawnedPlayerNetwork.gameObject.SetActive(true);
    11.         _SpawnedPlayerNetwork.NetworkObject.SpawnWithOwnership(OwnerClientId, true);
    12.         _SpawnedPlayerNetwork.Hero.Team = Team == 0 ? TeamEnum.Team1 : TeamEnum.Team2;
    13.     }
    14.  
    15.      
    16.  
    @Archimagus Try updating to the latest 1.2.0 this fixed issues i was having.
     
    Archimagus likes this.
  19. Archimagus

    Archimagus

    Joined:
    Sep 29, 2012
    Posts:
    21
    @daxiongmao Hey awesome, I am doing a similar thing, having the system spawn a player, and then that player spawns their avatar. After a quick check 1.2.0 seems to fix the issue, but I haven't had time to untangle my spaghetti of trying to make it work in the old system. Hopefully this is the fix. Thanks.
     
    daxiongmao likes this.
  20. Huhny

    Huhny

    Joined:
    Feb 8, 2017
    Posts:
    1
    upload_2023-3-24_16-41-57.png

    Re-enabling Reload Domain and Reload Scene seemed to be the fix for me (on 1.2.0)

    EDIT: Seems like only Reload Scene is required
     
    Last edited: Mar 24, 2023
  21. maxkcy

    maxkcy

    Joined:
    Oct 11, 2021
    Posts:
    62
    Hello, I am a beginner, and I am testing things out and learning them, hence the code my not follow strict guidelines, or good practices. Moreover, I still have this issue.

    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false, Delivery = RpcDelivery.Reliable)] // by default it is reliable.
    2.     public void SpawnNcVoogieServerRpc(ServerRpcParams serverRpcParams = default)
    3.     {
    4.         if (!IsServer) { Debug.Log("ConsoleNcgm: Is Not Server"); return; }
    5.         var instantiatedNcVoogieFromPrefab = Instantiate(_ncVoogie, Vector2.zero, Quaternion.identity);
    6.         instantiatedNcVoogieFromPrefab.GetComponent<MyVoogiesInputSystem>().enabled = true;
    7.         // ^ This only enables InputSystem on ServerSide, my "NcVoogie" gameObject spawns w/ default settings
    8.         var client = serverRpcParams.Receive.SenderClientId;
    9.         instantiatedNcVoogieFromPrefab.GetComponent<NetworkObject>().SpawnWithOwnership(client);
    10.         // ^ It has spawned on the server with client as owner.
    11.  
    12.         if (NetworkManager.Singleton.ConnectedClients.ContainsKey(client))
    13.         {
    14.             InitNcVoogieClientRpc(instantiatedNcVoogieFromPrefab.GetComponent<NetworkObject>().NetworkObjectId,              new ClientRpcParams
    15.             {
    16.                 Send = new ClientRpcSendParams
    17.                 {
    18.                     TargetClientIds = new ulong[] { client }
    19.                 }
    20.             });
    21.         }
    22.     }
    23. // Call a ClientRpc to init NcVoogie. In the future will run in OnNetworkSpawn, but since there are no NetworkBehaviors for testing, I am sending a clientRpc. ONS runs after this ClientRpc is sent and executed if that is even possible?
    24.     [ClientRpc]
    25.     private void InitNcVoogieClientRpc(ulong networkObjectId, ClientRpcParams clientRpcParams)
    26.     {
    27.         Debug.Log($"ConsoleNcgm.InitNcVoogieClient() call: A new NcVoogie has spawned. Am I the Owner?                 {IsOwner}");
    28. // ^ Prints on client side: "ConsoleNcgm.InitNcVoogieClient() call: A new NcVoogie has spawned. Am I the Owner? false"
    29. // for the client that is set to owner.
    30.         if (!IsOwner) return;
    31.         var cinCam = FindObjectOfType<CinemachineVirtualCamera>();
    32.         if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(networkObjectId, out NetworkObject ncFollowVoogie))
    33.         {
    34.             cinCam.Follow = ncFollowVoogie.transform;
    35.             ncFollowVoogie.GetComponent<MyVoogiesInputSystem>().enabled = true;
    36.             // ^ Have to enable this on client side depite trying to set it on server side first.
    37.         }
    38.     }
    Edit: sorry for any confusion, but console is networkobject owned by server. I am making fixes right now. To clear up further misinfo, I did run with a test OnNetworkSpawn, and that is called before the ClientRpc.
    Ok this works, Everything is working in version 1.2.0, in case anyone is interested:

    Code (CSharp):
    1. [ClientRpc]
    2.     private void InitNcVoogieClientRpc(ulong networkObjectId, ClientRpcParams clientRpcParams)
    3.     {
    4.         if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(networkObjectId, out NetworkObject ncFollowVoogie))
    5.         {
    6.             Debug.Log($"ConsoleNcgm.InitNcVoogieClientRpc() call: A new NcVoogie has spawned. Am I the Owner? {ncFollowVoogie.IsOwner}");
    7.             if (!ncFollowVoogie.IsOwner) return;
    8.             var cinCam = FindObjectOfType<CinemachineVirtualCamera>();
    9.             cinCam.Follow = ncFollowVoogie.transform;
    10.             ncFollowVoogie.GetComponent<MyVoogiesInputSystem>().enabled = true;
    11.         }
    12.     }
     
    Last edited: Apr 10, 2023
  22. KingSlayer5986

    KingSlayer5986

    Joined:
    Mar 29, 2023
    Posts:
    2
    I have an issue, when I spawn the first player as a host, it works fine, but when I spawn the second player as a client, it seems to control both the client and the host and the host has no control over himself, i looked into the player clones in hierarchy, one of them has isowner set to true and the other has it set to false(if that is relevant), here is the whole project if you need it: https://drive.google.com/drive/folders/1RCvA0bXsrmIIgo_s3M_4F4emmrKlTImB?usp=sharing
    please note: I am not an expert at unity, I started about two years ago and I am completely new to multiplayer
    also, I am using netcode 1.2.0 for the sake of following codemonkey's netcode tutorial
     
  23. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @KingSlayer5986 , you probably have the character controller script / Input System enabled by default on the player's prefab. There are a few examples you can look at to see how to initialize players movement properly:

    Have you already had a look at the examples in the official ClientDriven sample? (Download here)

    It show how to use the third person controller asset in a multiplayer environment for movement and picking objects up.

    Does this help?