I have some server-side code that instantiates a prefab, sets some properties on it, and then spawns it on the network. On the client, I would like to run some logic to initialize some client-only components of this object. I would have expected that I could do this from OnStartClient(), but that appears to get called before the object’s SyncVars have been replicated. Here is a simplified example of what I’m trying to do:
class MyComponent : NetworkBehaviour
{
[SyncVar]
public GameObject MyObj;
[SyncVar]
public int MyInt;
}
...
// Some code running on the server...
GameObject somePrefabReference;
GameObject foo = Instantiate(somePrefabReference);
foo.GetComponent<MyComponent>().MyObj = this;
foo.GetComponent<MyComponent>().MyInt = 42;
NetworkServer.Spawn(foo);
...
// Some component on the object spawned above...
public override void OnStartClient()
{
var myObj = GetComponent<MyComponent>().MyObj;
var myInt = GetComponent<MyComponent>().MyInt;
DoSomeComplicatedLogic(myObj, myInt);
}
Unfortunately, it appears that at the time OnStartClient() is called, the properties of MyComponent have not been replicated yet, so myObj is null. It seems reasonable that OnStartClient might be expected to run before the object has finished spawning/syncing, but if that were the case, I’d expect there to be another callback I can use to run some logic after all of the variables have been replicated. Where is the proper place for me to call DoSomeComplicatedLogic() such that I can be sure all of the variables have already been replicated.
I can’t put this logic inside a sync var hook for any of the properties in MyComponent, because the logic needs to wait to run until all of the SyncVars have been replicated and I don’t know which order they’ll be synced in. Also, it’s initialization logic and I don’t necessarily want it to run if the value of that SyncVar changes later on.
Thanks for any help!