Trying to understand the [SyncVar] in combination with GameObject.
The idea is that objects in my game retain a reference to the player who owns them. I have a syncvar that points to the owning player.
When a player disconnects, I was previously setting owner = null but this does not trigger the syncvar update hook. So - I made a global shared GameObject with a NetworkIdentity that I designate the ‘null’ object, instead. So now I can set owner = nullObject on the server which triggers the syncvar hook.
But, when I set owner = nullObject I get some behaviour that I don’t quite understand.
[SyncVar(hook="_OnOwnerChange")]
public GameObject owner;
void _OnOwnerChange( GameObject newOwner ) {
owner = newOwner;
Debug.Log("OnOwnerChange - owner = " + owner.ToString());
Debug.Log("OnOwnerChange - newOwner = " + newOwner.ToString());
OnOwnerChange(); // done this way so OnOwnerChange can be overriddden!
}
With the result in the console:
OnOwnerChange - owner = mercior (UnityEngine.GameObject)
OnOwnerChange - newOwner = NullObject (UnityEngine.GameObject)
I don’t fully understand how the 2 variables can be different? This is in the console of a HOST that I am testing with (rather than a dedicated server) so perhaps I am seeing this message from the client-side rather than the server? And then the client can’t set ‘owner’ because it is a syncvar??
If so… is there any way I can get around this using a StartHost() setup? Is the SyncVar change hook called at all on the server? Its going to slow development for me a lot if I have to start building a dedicated server and client separately every time I want to test so I am trying to find a solution that will allow me to keep testing in the editor.