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?
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.
Could someone report a bug on Github and if possible include some quick code to replicate it and explain how you spawned the object? Sign in to GitHub · GitHub
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
I’m having the same issue. How did your workaround work?
I’m considering just using (OwnerClientID == NetworkManager.Singleton.LocalClientId) at this point
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.
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (IsServer)
{
var local_player = GameObject.Instantiate(_PlayerPrefab);
local_player.name = string.Format("Player {0}", OwnerClientId);
_SpawnedPlayerNetwork = local_player.GetComponent<PlayerNetwork>();
_SpawnedPlayerNetwork.gameObject.SetActive(true);
_SpawnedPlayerNetwork.NetworkObject.SpawnWithOwnership(OwnerClientId, true);
}
}
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.
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)
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:
_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).
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)
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.
In my setup that code was on the Player Spawner Prefab.
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.
@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.