Setting up the Scene Spawner
5 - 10 minutes read
At this point, you have the Player RealTime Agent set up. Select the Player GameObject in the Scene Hierarchy and drag it into the Prefabs folder in the Project Assets folder.
Add a new Empty GameObject and name it "SceneSpawner". Click the Add Component button and Search "Spawner", select Scene Spawner to attach it to the Player GameObject.
You will see that a Network System ID component was attached to SceneSpawner GameObject automatically. Network System ID component help SWNetwork to identify SWNetwork's internal GameObjects.
SceneSpawner instantiates and destroys Networked GameObject in a Scene(GameObjects that have a Network ID components). If you have multiple scenes in your game, you need to add a SceneSpawner for each of them. Make sure you give different Spawner Id to different SceneSpawners because SceneSpawner stores important GameObject Spawn History of a scene. GameObject Spawn History is used to restore GameObjects when players disconnected and reconnected to a game.
Our game only has one game scene, we can set the Spawner Id to 1.
Create an Empty GameObject and name it "SpawnPoints", reset its position to (0, 0, 0).
Create an Empty GameObject and name it "SpawnPoint", reset it position to (0, 0, 0).
Select an Icon for the SpawnPoint so we can easily find it in the Scene.
Select the SpawnPoint GameObject in the Hierarchy and drag it into the Prefabs Folder to create a SpawnPoint Prefab.
Drag the SpawnPoint prefab into the SpawnPoints GameObject and create 4 SpawnPoints.
Move the SpawnPoints to the places you like to spawn the players. Set the Position Y to 1.5 to make sure the players are spawned above the ground.
Select the SceneSpawner GameObject, open the Spawn Point section and set the Size to 4. Drag the SpawnPoint into the slots.
Open the Player Prefabs section of the SceneSpawner, drag the Player Prefab into it.
Open the GameSceneManager.cs script, and add a method to handle the On Ready Event of SceneSpawner.
Your GameSceneManager.cs script should look like:
/// Game scene manager manages game scenes state.
public class GameSceneManager : MonoBehaviour
public GameObject winnerPanel;
public GameObject gameOverPanel;
public void Exit()
// exit the game
// OnSpawnerReady(bool alreadySetup) method is added to handle the On Ready Event.
public void OnSpawnerReady(bool alreadySetup, SceneSpawner sceneSpawner)
Debug.Log("OnSpawnerReady " + alreadySetup);
// Check alreadySetup to see if the scene has been set up before.
// If it is true, it means the player disconnected and reconnected to the game.
// In this case, we should not spawn a new Player GameObject for the player.
// If alreadySetup is false, it means the player just started the game.
// We randomly select a SpawnPoint and ask the SceneSpawner to spawn a Player GameObject.
// we have 1 playerPrefabs so playerPrefabIndex is 0.
// We have 4 spawnPoints so we generated a random int between 0 to 3.
int spawnPointIndex = Random.Range(0, 3);
// Tell the spawner that we have finished setting up the scene.
// alreadySetup will be true when SceneSpawn becomes ready next time.
In the SceneSpawner component, add a listener to the On Ready event. Select OnSpawnerReady(bool, SceneSpawner) in the GameSceneManager.cs script.
Make sure to select the OnSpawnerReady method from the Dynamic bool SceneSpawner section in the list.