- Home /
Instantiated objects not showing on new clients [uNet]
I'm not the most skilled developer so I rely on tutorials a lot, however I can't figure out this thing. I'm making a 2D multiplayer survival game with zombies that automatically spawn. That works fine, but when a new client joins the game, the zombies that had previously been instantiated won't show on that client. Here's my code:
public class EnemySpawner : NetworkBehaviour { public float speed; public GameObject enemy;
void Start()
{
CmdInvokeSpawnEnemy();
}
[Command]
void CmdInvokeSpawnEnemy()
{
InvokeRepeating("RpcSpawnEnemy", 1, speed);
}
[ClientRpc]
void RpcSpawnEnemy()
{
float x = Random.Range(-20, Camera.main.pixelWidth + 20);
float y = Random.Range(-20, Camera.main.pixelHeight + 20);
Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 0f));
target.z = 0f;
GameObject enemyToSpawn = Instantiate(enemy, target, Quaternion.identity);
NetworkServer.Spawn(enemyToSpawn);
//Instantiate(enemy, target, Quaternion.identity);
}
}
Could anyone alter my code to show me the fix please?
Jared
Answer by ThaiCat · Nov 11, 2017 at 10:33 AM
NetworkServer.Spawn(enemyToSpawn); should be called on server, you call it on connected clients, because it is in [ClientRpc] void RpcSpawnEnemy()
Right, also Commands are ment to be methods that are run on the server and are called from clients while ClientRPCs are methods that run on the clients but are called from the server. I doubt you want a client to actually call the "CmdInvokeSpawnEnemy" method on the server.
Also keep in $$anonymous$$d that the object that is instantiated and "Spawned" need to have a NetworkIdentity component.
Your answer
![](https://koobas.hobune.stream/wayback/20220612142610im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Network.Instantiate issues - GameObject ownership and network views? 0 Answers
Instantiate prefab, strange problem 1 Answer
Having problem with making non networked objects only be intractable with local player 0 Answers
Objects instantiated by the host show on all clients but client objects don't show on host. 0 Answers
Network Instantiate Prefab Identifier 0 Answers