Spawn prefabs
Spawner
is used to instantiate prefabs and share it to other clients.
It works like other variables.
NetSpawner
has 2 fields:
- The prefab
- Parent transform (instantiated objects will be in)
In this example, we instantiate a prefab with keyboard.I
and destroy one with keyboard J
.
public NetSpawner Spawner;
void Awake() {
Spawner.Init(this);
}
void Update() {
if (Spawner.IsActive) {
if (Input.GetKeyDown(KeyCode.I)) {
Spawner.Instantiate();
}
if (Input.GetKeyDown(KeyCode.J)) {
// select a random instance to remove
var go = Spawner.GetInstance((byte) Random.Range(0, Spawner.Count));
if (go != null) {
go.SafeDestroy();
}
}
}
}
NetworkedObjects in a prefabs have their own IDs, automatically updated. If you put a prefab in a scene, these IDs will be changed.