I know this is an old post, but i wanted to shed some light on this topic as most newcomers still get lost when it comes to identifying the client and server objects associated with COMMAND’s and RPC’s.
I wanted to share what i do in case it helps others still:
I made a NetworkObject class that i use as a baseclass for all my other networkbehavior scripts, such as Item,Interactable Scenery, Chests, Npc, Player, etc
using Mirror;
using System.Collections.Generic;
public class NetworkObject : NetworkBehaviour {
// Dictionary to store spawned gameobjects by netId | Usage: spawnedNetworkObjects[netId]
public static Dictionary<uint,NetworkObject> spawnedNetworkObjects = new Dictionary<uint, NetworkObject>();
// Add this object to the dictionary
public virtual void Start()
{
spawnedNetworkObjects.Add(netId, this);
}
// Remove this object from the dictionary
public virtual void OnDestroy()
{
spawnedNetworkObjects.Remove(netId);
}
}
Example of it in use from a Player
public class Player : NetworkObject
{
public override void Start()
{
base.Start();
if (isServer)
Debug.Log($"Find Myself On server: {spawnedNetworkObjects[netId].name}");
if (isLocalPlayer)
{
Debug.Log($"Find Myself On Local Client: {spawnedNetworkObjects[netId].name}");
Debug.Log($"Same as this: {name}");
}
}
}
This can also be used from other classes or commands as well using the full static method
NetworkObject.spawnedNetworkObjects[netId];
And you can use it to find Components attached to the object just like any other GameObject.
NetworkObject.spawnedNetworkObjects[netId].GetComponent<T>();
NetworkObject.spawnedNetworkObjects[netId].transform;
NetworkObject.spawnedNetworkObjects[netId].gameObject;
// Etc
I tend to use this method when i need a client to send a command to the server, with a netID and any additional parameters of the object im interacting with, then i can just look it up with NetworkObject.spawnedNetworkObjects[netId].
Again i know this post was old, but i wanted to elaborate more as this was one of the first results on google when it comes to this topic.
If you want more information let me know.