1

So I spawn players and than guns that they have, on a host it works perfectly but on a client guns do not become instance of an object. Here's the code

 [Command]
 void CmdSpawn() {
     gun = (GameObject)Instantiate(gunToEquip, weaponPosition.position, weaponPosition.rotation);

     NetworkServer.SpawnWithClientAuthority(gun, connectionToClient);
     gun.transform.parent = weaponPosition;
 }
user2088880
  • 39
  • 2
  • 8

2 Answers2

2

First of all, you aren't setting the parent on client. Only on the server. The host is server and client at the same time so it works. Second - have you dragged a gun prefab into network manager's spawnable prefabs slot?

Also, I believe you shouldn't be doing it like so:

gun.transform.parent = weaponPosition;

Use that instead:

gun.transform.SetParent(weaponPosition);

Try that:

[Command]
void CmdSpawn() 
{
    gun=(GameObject)Instantiate(gunToEquip,weaponPosition.position,weaponPosition.rotation);
    gun.transform.SetParent(weaponPosition);
    RpcSpawn();
}

[ClientRpc]
void RpcSpawn()
{
    if(NetworkServer.active) return;
    gun = (GameObject)Instantiate(gunToEquip, weaponPosition.position, weaponPosition.rotation);
    gun.transform.SetParent(weaponPosition);
}
  • Yes I dragged a gun prefab into network manager's spawnable prefabs, so how to make it spawn on a client too? – user2088880 May 22 '16 at 19:46
  • Oh, it isn't spawning at all? Try NetworkServer.Spawn(gun); – Dmitrij Kovaliov May 22 '16 at 19:47
  • 1
    I'm sorry, that was stupid, how to make it spawn on a client as a child of weaponPosition, because it spawns, but just stays in the middle of the map for both player on a client, meanwhile host does this properly – user2088880 May 22 '16 at 19:49
  • I actually send an rpc to spawn an object, which needs to be parented to something else(and don't use NetworkServer.Spawn then). But be careful, make sure not to spawn the object twice(because host is 2 in 1 - client and server) You can check NetworkServer.active inside and rpc - if it is true - abort – Dmitrij Kovaliov May 22 '16 at 19:54
  • Sooo, how to spawn it so it becomes a child of WeaponPosition? – user2088880 May 22 '16 at 20:00
  • Edited my answer. Hope it helps – Dmitrij Kovaliov May 22 '16 at 20:10
  • Ok so it works, partially, (thanks for that), but now when Client connects it has 2 guns and host has no gun, on host screen it works as intendeed – user2088880 May 22 '16 at 20:23
  • Okay it almost works, now the player that joins later can't see the weapon of players already connected, that way host sees all weapons, player connected second sees only his, but when third player joins, he sees his weapon aswell. – user2088880 May 22 '16 at 20:42
  • Yeah you might need to implement a state updater. Like, when player joins the game he requests other player's weapon and spawns it. – Dmitrij Kovaliov May 23 '16 at 13:29
  • Also, you migth want to take a look at NetworkMessages: http://docs.unity3d.com/Manual/UNetMessages.html Theese are extreemely useful in some cases and give you a lot of control, since you can decide which client needs to receive a message(remember, RPC's are sent to all clients) – Dmitrij Kovaliov May 23 '16 at 13:44
2

It seems to be a duplicate of Unity 5.1 Networking - Spawn an object as a child for the host and all clients

The idea discussed there is to synchronize network ID of the parent object, e.g. on the Server side while spawning:

var gun = Instantiate (...);
gun.parentNetId = this.netId;
NetworkServer.Spawn (gun);

And later on the clients:

// GunController.cs
[SyncVar]
public NetworkInstanceId parentNetId;

public override void OnStartClient()
{
    GameObject parentObject = ClientScene.FindLocalObject(parentNetId);
    transform.SetParent(parentObject.transform);
}
Community
  • 1
  • 1
romaroma
  • 658
  • 8
  • 13