Network.Instantiate causes lag in-editor

Really bizarre issue here. I have some AISpawner instances that handle server-side instantiation of enemies. The code is incredibly simple, and works without a hitch across Windows, Mac, iOS and Android:

var ai = Network.Instantiate(AIPrefab, Vector3.zero, Quaternion.identity, 0) as GameObject;
var controller = ai.GetComponent<AIController>();
controller.Init(GetSpawnPosition());

AIPrefab is an inspector variable, and GetSpawnPosition just grabs a random position from within the spawn area; Init just does some post-spawn setup. This is only ever called on the server.

For some weird reason, this causes a good second’s worth of lag, but only when I’m playtesting in the editor; works smoothly as expected even on mobile. I’m using Unity Free and the basic mobile licences, so I can’t profile.

Any thoughts? :slight_smile:

Is network debugging enabled? Also try hiding the inspector (put something else on a different tab, and switch to that tab) as it has a huge impact on performance.

You can also use System.Diagnostics.Stopwatch to time regions of code and find out exactly which bit is being slow, e.g. is the lag occuring on the Instantiate call, or is it occuring at the end of the frame.

Oh of course, network debugging :slight_smile: Had it set to informational, totally forgot about the log overhead from that, particularly when I sent a few RPCs at once in other areas (and my own log callback contributes too). Cheers!