com.unity.netcode.gameobjects
com.unity.netcode.gameobjects copied to clipboard
IsOwner is false on clients for player objects during OnNetworkSpawn()
Description
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.
Reproduce Steps
- Create a new NetworkBehavior
- Override OnNetworkSpawn() with something like: public override void OnNetworkSpawn() { base.OnNetworkSpawn(); Debug.Log($"IsOwner: {IsOwner}"); }
- Create a prefab with the new NetworkBehavior script
- Make that prefab the default PlayerObject
- Start a host, observe the output of the OnNetworkSpawn() debug log line.
- Start a client, observe the output of the OnNetworkSpawn() debug log line.
Actual Outcome
The client log line is: "IsOwner: false"
Expected Outcome
The client log line is: "IsOwner: true"
Screenshots
N/A
Environment
- OS: Windows 10
- Unity Version: 2021.2.16f
- Netcode Version: 1.0.1
- Netcode Commit: [e.g. https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/commit/ba418fa5b600ad9eb61fab0575f12fbecc2c6520]
Additional Context
This looks like a possible regression as IsOwner used to be set correctly during OnNetworkSpawn().
@dsaunder I just ran this test using this script:
public override void OnNetworkSpawn()
{
var instanceType = IsServer ? "Server" : "Client";
if (IsOwner)
{
Debug.Log($"[{instanceType}] Local player ({NetworkObject.OwnerClientId}) spawned!");
}
else
{
Debug.Log($"[{instanceType}] Remote player ({NetworkObject.OwnerClientId}) spawned!");
}
}
This was the output I got:
[Client] Remote player (0) spawned! [Client] Local player (2) spawned!
Does your script look similar to the above? If not, then make sure you are checking:
- if the NetworkObject spawning belongs to the local player
- what the NetworkObject's OwnerClientId is to determine which instance is being spawned on the client
you can also do something like this:
if (NetworkObject.OwnerClientId == NetworkManager.LocalClientId)
{
Debug.Log("This is the local player's NetworkObject.");
}
If you are trying to only execute code specific to the local player's instance.
Let me know if this resolves your issue?
I thought i was having the same issue. I tried the debug script you provided and it seems that the client IsOwner. Although I still get "Client is not allowed to write to this networkvariable" when triyng to move. when editor is client.
If i have the server in the editor i get: "Trying to update interpolator when no data has beed added to it yet".
I am using the ClientNetworktTransform.
I Debuggd all the way trough the "TrycommitTransformToServer" method in ClientNetworkTransform and i found this.
` public bool CanClientWrite(ulong clientId)
{
switch (WritePerm) --> (Writeperm = "server" in this switch)<--
{
default:
case NetworkVariableWritePermission.Server:
return clientId == NetworkManager.ServerClientId;
case NetworkVariableWritePermission.Owner:
return clientId == m_NetworkBehaviour.NetworkObject.OwnerClientId;
}
}`
I suppose this is supposed to be "Owner" so that my client have writeperm? But i cant figure out why its just server that has writeperm. I thought the ClientNetwork transform made it so that the owner hade permission?
EDIT You can override the OnIsServerAuthorative as such: "protected override bool OnIsServerAuthoritative() "
and just return false to manually set it to not be ServerAuthoritative. That did it for me. Although i think this should not be neccessary. Haven't seen anyone do this when checking tutorials and such for anwsers...
Hope that helps someone in the future!
@BaloghDaniel Yeah, I see that we are missing some information in that NetworkTransform documentation on this indeed. I went ahead and submitted a PR to provide more details on this.
As a side note, the most recent version of the ClientNetworkTransform does actually have the OnIsServerAuthorative method.
I am going to close this issue as being resolved at this time. If you have any other questions regarding NetworkTransform please feel free to post here and I will do my best to assist you.