On my player object i can spawn an object with client authority like
[Command]
void CmdSpawnNewObj() {
var newObjectInstantiated = (GameObject)Instantiate (newObjectPrefab, transform.position, transform.rotation);
NetworkServer.SpawnWithClientAuthority (newObjectInstantiated, connectionToClient);
}
Then on the network behavior script attached to the new object I would expect both of the following to run on the client whose player object called the command to spawn, and not on the others:
void OnStartAuthority() {
//this should run
}
void Update() {
if(hasAuthority){
//this should also run
}
}
This works for the players who are not the host. It does not work for the player who is the host, in which case the above lines will not run.
The following has been suggested in past threads:
- If the player is a host, I should use
NetworkServer.Spawn
instead of NetworkServer.SpawnWithClientAuthority
- If the player is a host, in the spawn method, I should replace
connectionToClient
with gameObject
Neither of these work.
Update, partially solved.
The following is still not running at all on a host player in the above scenario:
void OnStartAuthority() {
//this should run
}
The checks in the update function will actually run as follows:
void Update() {
if(!hasAuthority){
//this will run once
}
if(hasAuthority){
//this will run every frame
}
}
…so, the server portion of the host runs a single iteration of update on object creation and then subsequent updates are treated as client? This seems strange, is it this expected behavior? Anyway, my problem was caused by the first, check changing something earlier in the code. Just have to make sure to account for this.
I don’t believe you should spawn objects with server authority using NetworkServer.SpawnWithClientAuthority.
Well I’m not trying to give the server authority on the object. The system acting as a host is both a server and a client, I’m trying to give the client portion authority like any other client.
But there is no concept of a “client portion”. If you’re the host, you are the server. If you want that host to have authority over an object, you are spawning with server authority.