NetworkServer.Spawn() only spawning on server

Hey guys!

So I have a simple Server and a client.

A spawner script to spawn objects over the network is on the map:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class EnemySpawner : NetworkBehaviour {

    public GameObject objectToSpawn;

    private float t;

    void Start () {
        //t = Time.time + 15f;
    }

    public override void OnStartServer() {
        t = Time.time + 15f;
    }
   
    void FixedUpdate () {
        if (!isServer)
            return;
        if (Time.time > t) {
            CmdSpawnObject();
            t = Time.time + 15f;
        }
    }

    [Command]
    void CmdSpawnObject() {
        GameObject go = GameObject.Instantiate(objectToSpawn, transform.position, Quaternion.identity) as GameObject;
        NetworkServer.Spawn(go);
    }
}

So it will actually spawn the object on the server however no clients will spawn the object :frowning:
How can I fix this please?
PS: This spawner object has a network identity on it also.

Dang does no one have any help for me?:confused:
I need this working asap please :frowning:

The NetworkManager needs to understand that you want it to be tracked in the network so you need to register the GameObject.

You can do it by code but you can also go into your NetworkManager’s gameObject and add your prefab “objectToSpawn” into the list of objects that the NetworkManager can handle. I don’t have Unity in front of me right now but the option should be inside a collapse menu in the inspector once you select your NetworkManager.

1 Like