B4iT
B4iT

Reputation: 1

Can't spawn network object

I tried spawning a prefab I made, and I keep getting the following error message: "NotListeningException: NetworkManager is not listening, start a server or host before spawning objects". The prefab IS a network object and is attached to the player objects script called "spawner". The player object prefab is linked to the network manager's Player field.

The Spawner script:

public class Spawner : NetworkBehaviour
{
    private string _playerID;
    public NetworkObject prefab;

    public override void OnNetworkSpawn()
    {
        _playerID = NetworkManager.LocalClientId.ToString();

    }

    private void Update()
    {
        if (Input.anyKey)
        {
            Debug.Log(_playerID);
            prefab.GetComponent<NetworkObject>().Spawn();
        }
    }
}

(https://i.sstatic.net/zzWBX.png)

I start a host after entering play mode, and the player prefab gets spawned, so I guess the server should already be started at the time the spawning should happen.

Upvotes: 0

Views: 207

Answers (1)

MrWatchmanLP
MrWatchmanLP

Reputation: 33

You should instanstiate your prefab first.

private void Update()
{
    if (Input.anyKey)
    {
        Debug.Log(_playerID);
        var spawnedObject = Instantiate(prefab);
        spawnedObject.GetComponent<NetworkObject>().Spawn();
    }
}

Upvotes: 0

Related Questions