- Home /
 
Syncronization issues between server and client, using the OnStartServer() function,
Hi, I'm new to Unity, and I'm having troubles for synchronisation between server and clients. I am running the following code to spawn enemies at random positions.
//code starts here
using UnityEngine;
using UnityEngine.Networking;
public class EnemySpawner : NetworkBehaviour
{
 public GameObject enemyPrefab;
 public int numberOfEnemies;
 
 public override void OnStartServer()
 {
     for (int i = 0;i<numberOfEnemies; i++)
     {
         var spawnPosition = new Vector3(
             Random.Range(-8.0f, 8.0f),
             0.0f,
             Random.Range(-8.0f, 8.0f));
         var spawnRotation = Quaternion.Euler(
             0.0f,
             Random.Range(0,180),
             0.0f);
         var enemy = (GameObject)Instantiate(enemyPrefab,spawnPosition,spawnRotation);
         NetworkServer.Spawn(enemy);
         Debug.Log("spawn 1");
          
     }
 }
 
               }
When I launch a game through the 'play' button in unity, then start to host a server, enemy spawn properly. However, after been built and run, the new client doens't show any enemy. I am sure that enemies exist on the server because they can be shot from the second client (even without seeing them).
Now if I try to host the server from the built and run version, nothing happens, and enemy are not present on any of the two clients. Furthermore, I can't even acces the console to see if the "OnStartServer" fonctions works properly as there is no console in the built version...
I created my 'enemy' prefab from the 'player' one, and the player works well in both clients. Those prefabs have a Network Identity, (+ Network Transform for the player), and are registred as 'spawn' in my network manager. The player is created each time a new client is opened. For the ennemy spawn, I have an object called 'Enemy Spawner', placed in my scene. It contains the previous code, and a Network Identity.
I have tryed to change the Netwotk Identities of the enemy prefab and the enemy spawner, leading to no real results.
Thanks for your time
Your answer