- Home /
How do clients Use 'NetworkServer.Spawn ();'?
I want the client to create the object
But 'NetworkServer.Spawn ();' it seems only server role
How do I make the client to create was built as a server?
Answer by n1gth · Jul 07, 2015 at 06:25 AM
Ideally, the server should create the object, then all the other clients will know about it too. The client player could use a Command to tell the server to do this.
[Client] // called only on client
public void SpawnObject(Vector3 location)
{
CmdSpawn(location);
}
[Command] // runs only on server
private void CmdSpawn(Vector3 location)
{
var obj = (GameObject)Instantiate(MyPrefab, location, someRotation);
NetworkServer.Spawn(obj);
}
This assumes that you have NetworkIdentity on your prefab, and that the Player object this is in is a NetworkBehaviour.
it worked! Thank you!
I have a better understanding of his principles
Thank you: D
If I use a command and inside I call a RpcClient function to instantiate an onbject. I am using only the Instantiate method and it is working, so why use NetworkServer.Spawn? Dont got when i have to use it. What is the difference?
NetworkSever.Spawn will instantiate the object on server as well, therefore the object is a 'networked' object. Your method will only instantiate objects on all clients expect server, in other worlds, they are different local objects ins$$anonymous$$d of a 'networked' object, which means networkbehavior won't work in this case.
This way can be useful if you spawn things like 'explosion animation' which doesn't affect game world, but if you want to interact with this object, for example 'pushing' it, 'destroying' it etc. It will behave differently from client to client.
Your answer
![](https://koobas.hobune.stream/wayback/20220611230042im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# How to make the gun remember who equipped it 1 Answer
Networked Game Issues - Camera does not follow script 1 Answer
Networking Synchronize Problem. 0 Answers