M
M
Mirror
Search…
Pickups, Drops, and Child Objects
Frequently the question comes up about how to handle objects that are attached as children of the player prefab that all clients need to know about and synchronize, such as which weapon is equipped, picking up networked scene objects, and players dropping objects into the scene.
Mirror cannot support multiple Network Identity components within an object hierarchy. Since the Player object must have a Network Identity, none of its descendant objects can have one.

Child Objects

Let's start with the simple case of a single attachment point that is somewhere down the hierarchy of our Player, such as a hand at the end of an arm. In a script that inherits from NetworkBehaviour on the Player Prefab, we'd have a GameObject reference where the attachment point can be assigned in the inspector, a SyncVar enum with various choices of what the player is holding, and and a Hook for the SyncVar to swap out the art of the held item based on the new value.
In the image below, Kyle has an empty game object, RightHand, added to the wrist, and some prefabs to be equipped (Ball, Box, Cylinder), and a Player Equip script to handle them.
NOTE: The item prefabs are art only...they have no scripts, and they must not have networking components. They can have monobehaviour-based scripts, of course, which can be referenced and called from ClientRpc's on the player prefab.
The inspector shows RightHand assigned in 2 places, the Player Equip script, as well as the target of the Network Transform Child component, so we could adjust the relative position of the attachment point (not the art) for all clients as needed.
Below is the Player Equip script to handle the changing of the equipped item, and some notes for consideration:
  • While we could just have all the art items attached at design time and just enable / disable them based on the enum, this doesn't scale well to a lot of items and if they have scripts on them for how they behave in the game, such as animations, special effects, etc. it could get ugly pretty fast, so this example locally instantiates and destroys instead as a design choice.
  • The example makes no effort to deal with position offset between the item and the attach point, e.g. having the grip or handle of an item align with the hand. This is best dealt with in a monobehaviour script on the item that has public fields for the local position and rotation that can be set in the designer and a bit of code in Start to apply those values in local coordinates relative to the parent attach point.
1
using UnityEngine;
2
using System.Collections;
3
using Mirror;
4
5
public enum EquippedItem : byte
6
{
7
nothing,
8
ball,
9
box,
10
cylinder
11
}
12
13
public class PlayerEquip : NetworkBehaviour
14
{
15
public GameObject sceneObjectPrefab;
16
17
public GameObject rightHand;
18
19
public GameObject ballPrefab;
20
public GameObject boxPrefab;
21
public GameObject cylinderPrefab;
22
23
[SyncVar(hook = nameof(OnChangeEquipment))]
24
public EquippedItem equippedItem;
25
26
void OnChangeEquipment(EquippedItem oldEquippedItem, EquippedItem newEquippedItem)
27
{
28
StartCoroutine(ChangeEquipment(newEquippedItem));
29
}
30
31
// Since Destroy is delayed to the end of the current frame, we use a coroutine
32
// to clear out any child objects before instantiating the new one
33
IEnumerator ChangeEquipment(EquippedItem newEquippedItem)
34
{
35
while (rightHand.transform.childCount > 0)
36
{
37
Destroy(rightHand.transform.GetChild(0).gameObject);
38
yield return null;
39
}
40
41
switch (newEquippedItem)
42
{
43
case EquippedItem.ball:
44
Instantiate(ballPrefab, rightHand.transform);
45
break;
46
case EquippedItem.box:
47
Instantiate(boxPrefab, rightHand.transform);
48
break;
49
case EquippedItem.cylinder:
50
Instantiate(cylinderPrefab, rightHand.transform);
51
break;
52
}
53
}
54
55
void Update()
56
{
57
if (!isLocalPlayer) return;
58
59
if (Input.GetKeyDown(KeyCode.Alpha0) && equippedItem != EquippedItem.nothing)
60
CmdChangeEquippedItem(EquippedItem.nothing);
61
if (Input.GetKeyDown(KeyCode.Alpha1) && equippedItem != EquippedItem.ball)
62
CmdChangeEquippedItem(EquippedItem.ball);
63
if (Input.GetKeyDown(KeyCode.Alpha2) && equippedItem != EquippedItem.box)
64
CmdChangeEquippedItem(EquippedItem.box);
65
if (Input.GetKeyDown(KeyCode.Alpha3) && equippedItem != EquippedItem.cylinder)
66
CmdChangeEquippedItem(EquippedItem.cylinder);
67
}
68
69
[Command]
70
void CmdChangeEquippedItem(EquippedItem selectedItem)
71
{
72
equippedItem = selectedItem;
73
}
74
}
Copied!

Dropping Items

Now that we can equip the items, we need a way to drop the current item into the world as a networked item. Remember that, as child art, the item prefabs have no networking components on them at all.
First, let's add one more Input to the Update method above, and a CmdDropItem method:
1
void Update()
2
{
3
if (!isLocalPlayer) return;
4
5
if (Input.GetKeyDown(KeyCode.Alpha0) && equippedItem != EquippedItem.nothing)
6
CmdChangeEquippedItem(EquippedItem.nothing);
7
if (Input.GetKeyDown(KeyCode.Alpha1) && equippedItem != EquippedItem.ball)
8
CmdChangeEquippedItem(EquippedItem.ball);
9
if (Input.GetKeyDown(KeyCode.Alpha2) && equippedItem != EquippedItem.box)
10
CmdChangeEquippedItem(EquippedItem.box);
11
if (Input.GetKeyDown(KeyCode.Alpha3) && equippedItem != EquippedItem.cylinder)
12
CmdChangeEquippedItem(EquippedItem.cylinder);
13
14
if (Input.GetKeyDown(KeyCode.X) && equippedItem != EquippedItem.nothing)
15
CmdDropItem();
16
}
Copied!
1
[Command]
2
void CmdDropItem()
3
{
4
// Instantiate the scene object on the server
5
Vector3 pos = rightHand.transform.position;
6
Quaternion rot = rightHand.transform.rotation;
7
GameObject newSceneObject = Instantiate(sceneObjectPrefab, pos, rot);
8
9
// set the RigidBody as non-kinematic on the server only (isKinematic = true in prefab)
10
newSceneObject.GetComponent<Rigidbody>().isKinematic = false;
11
12
SceneObject sceneObject = newSceneObject.GetComponent<SceneObject>();
13
14
// set the child object on the server
15
sceneObject.SetEquippedItem(equippedItem);
16
17
// set the SyncVar on the scene object for clients
18
sceneObject.equippedItem = equippedItem;
19
20
// set the player's SyncVar to nothing so clients will destroy the equipped child item
21
equippedItem = EquippedItem.nothing;
22
23
// Spawn the scene object on the network for all to see
24
NetworkServer.Spawn(newSceneObject);
25
}
Copied!
In the image above, there's a sceneObjectPrefab field that is assigned to a prefab that will act as a container for our item prefabs. The SceneObject prefab has a SceneObject script with a SyncVar like the Player Equip script, and a SetEquippedItem method that takes the shared enum value as a parameter.
1
using UnityEngine;
2
using System.Collections;
3
using Mirror;
4
5
public class SceneObject : NetworkBehaviour
6
{
7
[SyncVar(hook = nameof(OnChangeEquipment))]
8
public EquippedItem equippedItem;
9
10
public GameObject ballPrefab;
11
public GameObject boxPrefab;
12
public GameObject cylinderPrefab;
13
14
void OnChangeEquipment(EquippedItem oldEquippedItem, EquippedItem newEquippedItem)
15
{
16
StartCoroutine(ChangeEquipment(newEquippedItem));
17
}
18
19
// Since Destroy is delayed to the end of the current frame, we use a coroutine
20
// to clear out any child objects before instantiating the new one
21
IEnumerator ChangeEquipment(EquippedItem newEquippedItem)
22
{
23
while (transform.childCount > 0)
24
{
25
Destroy(transform.GetChild(0).gameObject);
26
yield return null;
27
}
28
29
// Use the new value, not the SyncVar property value
30
SetEquippedItem(newEquippedItem);
31
}
32
33
// SetEquippedItem is called on the client from OnChangeEquipment (above),
34
// and on the server from CmdDropItem in the PlayerEquip script.
35
public void SetEquippedItem(EquippedItem newEquippedItem)
36
{
37
switch (newEquippedItem)
38
{
39
case EquippedItem.ball:
40
Instantiate(ballPrefab, transform);
41
break;
42
case EquippedItem.box:
43
Instantiate(boxPrefab, transform);
44
break;
45
case EquippedItem.cylinder:
46
Instantiate(cylinderPrefab, transform);
47
break;
48
}
49
}
50
}
Copied!
In the run-time image below, the Ball(Clone) is attached to the RightHand object, and the Box(Clone) is attached to the SceneObject(Clone), which is shown in the inspector.
The art prefabs have simple colliders on them (sphere, box, capsule). If your art item has a mesh collider, it must be marked as Convex to work with the RigidBody on the SceneObject container.

Pickup Items

Now that we have a box dropped in the scene, we need to pick it up again. To do that, a CmdPickupItem method is added to the Player Equip script:
1
// CmdPickupItem is public because it's called from a script on the SceneObject
2
[Command]
3
public void CmdPickupItem(GameObject sceneObject)
4
{
5
// set the player's SyncVar so clients can show the equipped item
6
equippedItem = sceneObject.GetComponent<SceneObject>().equippedItem;
7
8
// Destroy the scene object
9
NetworkServer.Destroy(sceneObject);
10
}
Copied!
This method is simply called from OnMouseDown in the Scene Object script:
1
void OnMouseDown()
2
{
3
NetworkClient.localPlayer.GetComponent<PlayerEquip>().CmdPickupItem(gameObject);
4
}
Copied!
Since the SceneObject(Clone) is networked, we can pass it directly through to CmdPickupItem on the player object to set the equipped item SyncVar and destroy the scene object.
For this entire example, the only prefab that needs to be registered with Network Manager besides the Player is the SceneObject prefab.