NetworkServer.Spawn() make client crash to desktop when called in a loop
Hi everyone,
In my game, the world is generated server-side, so in my world generation script I have some loops to instantiate prefabs.
The problem is, when I call NetworkServer.Spawn in these loops, it makes the client crash to desktop (even when the client is in the editor). Here is a simple implementation exemple (code is server side) :
void GeneratePlanets()
{
for (int i = 0; i < planetCount; i++)
{
Planet planet = Factory.CreatePlanet();
NetworkServer.Spawn(planet.gameObject);
}
}
Here is another implementation I've tested :
public class Factory : NetworkBehavior
{
List<GameObject> instanciatedObjects = new List<GameObject>();
void Update()
{
// Spawn every object instanciated by the server on the last frame
foreach (var item in instantiatedObjects)
NetworkServer.Spawn(item);
instanciatedObjects.Clear();
}
}
This is an exemple of implementation, I've tested many approaches but it fail everytime. Maybe I've missed something ?
Thanks in advance.
Answer by T-Rocktopia · Oct 21, 2015 at 09:29 PM
Calling NetworkServer.Spawn inside an Update inside a foreach statement will definitely crash any application.
I HIGHLY recommend changing the Update function to Start or Awake.
Otherwise, my only other assumption is that after you call "instanciatedObjects.Clear()" in Update, the next frame happens and the Update call is trying to loop through the foreach but "instanciatedObjects" no longer has any objects to iterate through.
Your answer
Follow this Question
Related Questions
Photon crash 1 Answer
Need help for Player Spawning and Player speed control slider!! 2 Answers
How to spawn different player prefab in unity networking (UNET)? 0 Answers
uNet Spawning Objects 0 Answers
Setting parent on spawned object Unet 0 Answers