- Home /
Network.isServer / isClient not working
Hi all,
I'm attempting to instantiate prefabs to the network in a 2 player unity board game. My code is as follows (to be triggered with a button press):
public void spawnMelee()
{
if (isSpawned == false)
{
if (Network.isServer)
{
Debug.Log("Server");
GameObject newMelee = Instantiate(meleeUnitL, spawn0L.transform.position, spawn0L.transform.rotation) as GameObject;
NetworkServer.Spawn(newMelee);
isSpawned = true;
}
if (Network.isClient)
{
GameObject newMelee = Instantiate(meleeUnitR, spawn0R.transform.position, spawn0R.transform.rotation) as GameObject;
isSpawned = true;
NetworkServer.Spawn(newMelee);
}
}
else return;
}
I can get two players connected using the Network Manager, but neither one can spawn prefabs. If I remove the if(Network.isServer) then one player can spawn the prefab but it doesn't populate to the other. These prefabs both have NetworkIdentities, but they're not listed in the Network Manager under prefabs. As this is a UI driven multiplayer game, my Network Manager does not spawn any prefabs on load.
Any advice most welcome!
Answer by Ulci · Aug 16, 2016 at 09:27 PM
First you need to register all spawnable prefabs in your network manager. Otherwise spawning these prefabs won't work correctly.
Then you probably want to use one player as the host which means he is server and client at the same time. I guess you wanted to make the host to spawn left and the other client right. The Spawn() method only works on the server which makes the clients creating the prefab. So while you keep in mind that both players are clients (one of them the host), you could use a command which tells the server to spawn the object like this: http://answers.unity3d.com/questions/1001536/how-do-clients-use-networkserverspawn.html
Your answer
Follow this Question
Related Questions
'NetworkTranformChild' problem 1 Answer
Unity networking tutorial? 6 Answers
UNet changing player prefabs int the network lobby sample. 0 Answers
NetworkServer.dontListen not working - How do I stop clients connecting? 2 Answers
Can i make multiplayer game without unity multiplayer service ? 1 Answer