- Home /
Why are objects only spawning on host but not on remote clients?
Why is the following code not spawning objects on both host and remote clients? How can I modify this code to make objects spawn on both host and remote clients? Is there a better solution?
using UnityEngine;
using UnityEngine.Networking;
public class CatSpawner : NetworkBehaviour
{
public int numberOfCats;
public GameObject catPrefab;
public override void OnStartServer()
{
for (int i = 0; i < numberOfCats; i++)
{
var spawnPosition = new Vector3(
Random.Range(-64.0f, 100.0f),
106.0f,
Random.Range(34.0f, 184.0f));
var spawnRotation = Quaternion.Euler(
0.0f,
Random.Range(0, 180),
0.0f);
var cats = (GameObject)Instantiate(catPrefab, spawnPosition, spawnRotation);
NetworkServer.Spawn(cats);
}
}
}
My object Prefab has Network Identity and Network Transform component. The Game Object with the spawning script has Network Identity. I don't see the problem.
Answer by LK84 · Dec 28, 2016 at 10:23 AM
Remove the ServerOnly checkmark (and don't use the Local Player Authority checkmark either!) and register the prefab in the Network Manager!
You have to be kidding me. I spent 6 hours tonight making 7 different builds, waiting for them to upload to my server, I set up a second pc eventually so I could try to host here and connect locally so I could better see what why my character would never spawn, only to finally find out he was actually spawning just not on my client pc. So I search that after finally figuring out what was happening to find out it was a mother f* checkbox. I am new to networking in Unity obviously, I thought that since it was right above the "Local Authority" box that when it said "Server Only" It was referring to authority also, and since I am making an RPG I thought to myself, "well yeah, I definitely want server authority only". fml...
You don't need another PC / server to test multiplayer. Just run the host from the Unity Editor, and run the build and connect to your localhost (default 7777 I believe). This way you can test online functionality on one PC, while still being able to view the console.
Answer by marcrem · Dec 28, 2016 at 01:53 AM
I might be wrong, but you need to use [Command] on a void which name starts with Cmd.
https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html
Thanks for the response but I tried using Command too but I get the same results.
using UnityEngine;
using UnityEngine.Networking;
public class SpiderSpawner : NetworkBehaviour
{
public int numberOfSpiders;
public GameObject spiderPrefab;
void Start()
{
CmdSpawn();
}
[Command]
void CmdSpawn()
{
for (int i = 0; i < numberOfSpiders; i++)
{
var spiderPosition = new Vector3(
Random.Range(-64.0f, 100.0f),
106.0f,
Random.Range(34.0f, 184.0f));
var spiderRotation = Quaternion.Euler(
0.0f,
Random.Range(0, 180),
0.0f);
var spiders = (GameObject)Instantiate(spiderPrefab, spiderPosition, spiderRotation);
NetworkServer.Spawn(spiders);
}
}
}
Answer by ElijahShadbolt · Dec 28, 2016 at 06:35 AM
The 'SmallCat' GameObject's NetworkIdentity has 'Server Only' set to true. I don't know much about Unity's networking system, but I think 'Server Only' would indicate it only spawns on the Host/Server, wouldn't it?
I thought that was it too but even having Local only selected does not work: