I would like to sync up a class member that contains a reference to another game object of the same NetworkBehaviour subclass. Usually, it’s for keeping track of where the enemy is, and I want to keep a reference to it.
Here’s the following code:
public class Foo : NetworkBehaviour {
[SyncVar]
public Foo enemy;
}
But just that code itself crashes Unity editor. Does that mean you can’t keep track of an enemy unit in a multiplayer game? Are there any other workarounds?
The docs say syncvars can be primitive types or structs, I didn’t see a mention of GameObject or classes. There is edit or build time checking coming in 5.2:
Does that mean I have to look it up all the time whenever the Foo object is updated? And have to manually set them to the same object for both the server and the client?
you could have a syncvar int that is the netid, look it up when it is set the first time in a hook, then cache the reference to the gameobject. Just realize that it is possible that your cached gameobject reference may go null if that object is destroyed, but that should be under your control.