I am developing a character selection screen for a multiplayer game using Unity/Mirror. With this code, when I select a character and drag it into one of my player’s slots, the RPC function places the same prefab in the opposite slot on the opponent’s side. What I want is for the chosen player’s selection to only instantiate on the other client’s side. I thought about using isLocalPlayer in the RPC, but it doesn’t work, as when I put a debug there, I see that it’s always false on the server side and true on the client side. I have tried using netId, identity, and other properties of the NetworkBehaviour class, but I always get the same result.
using Mirror;
using UnityEngine;
public struct Enemy
{
public int slot;
public string warrior;
public Enemy(int slot, string warrior)
{
this.slot = slot;
this.warrior = warrior;
}
}
public class SyncSelectionScene : NetworkBehaviour
{
public TextMesh texto;
[SyncVar(hook = nameof(OnSetPlayerId))] uint playerId;
[SyncVar(hook = nameof(OnEnemyChange))] Enemy enemy;
public override void OnStartClient()
{
base.OnStartClient();
if (isServer)
playerId = connectionToClient.identity.netId;
}
private void Update()
{
}
[Command]
public void CmdSetEnemy(int slot, string warrior)
{
enemy = new Enemy(slot, warrior);
}
public void SetEnemy(int slot, string warrior)
{
if (isServer)
{
enemy = new Enemy(slot, warrior);
return;
}
CmdSetEnemy(slot, warrior);
}
public void OnSetPlayerId(uint oldId, uint newId)
{
texto.text = newId.ToString();
}
public void OnEnemyChange(Enemy oldEnemy, Enemy newEnemy)
{
if (isServer)
RpcUpdateOpponentCharacter(newEnemy.warrior, newEnemy.slot);
}
[ClientRpc]
void RpcUpdateOpponentCharacter(string warrior, int slot)
{
Enemies.PutEnemiesInScene(warrior, slot);
}
}