首页 > 解决方案 > Unable to use server command from client using unity mirror

问题描述

Im trying to make a smart usable object system.
When the player is getting close to one of the usable items he will be able to press a button and use this specific item. everything works except for the use command execution;

using UnityEngine;
using Mirror;

public class Usable : NetworkBehaviour
{
    public void Use(GameObject[] allUsables, Player CurrentClient, GameObject[] vents = null)
    {
        if (CurrentClient.isLocalPlayer)
        {
            //this.GetComponent<NetworkIdentity>().AssignClientAuthority(CurrentClient.GetComponent<NetworkIdentity>().connectionToClient);
            switch (this.name)
            {
                case string name when name.Contains("EmergencyButton"):
                    Debug.Log("Reached Log Point");
                    CmdEmergencyButton();
                    break;
                default:
                    Debug.LogError("Not possible!");
                    break;
            }
            //this.GetComponent<NetworkIdentity>().RemoveClientAuthority();
        }
    }

    [Command]
    private void CmdEmergencyButton()
    {
        RpcEmergency();
    }

    [ClientRpc]
    private void RpcEmergency()
    {
        //Activates emergency button effect
        GameObject.Find("EmergencyButton").transform.GetChild(0).gameObject.SetActive(true);
    }
}

And calling the use function like this:

[Client]
void ActivateUse()
{
    if(isLocalPlayer)
    {
        Debug.Log("Client using");
        //currUsables has all the nearby usable objects
        GameObject currentToUse = GameObject.Find(currUsables[0].name);
        //Using that object.
        currentToUse.GetComponent<Usable>().Use(allUsables, this, allVents);
    }
}

From player object.
When calling CmdEmergencyButton() nothing happens except for this warning in the console;
Click to see warning

Trying to send command for object without authority. Usable.CmdEmergencyButton
UnityEngine.Logger:Log(LogType, Object)

I have tried giving authority to the client to use command from this object (comments in use function) but still nothing.
Any idea why it doesn't work?

标签: c#unity3dgame-developmentunity-networking

解决方案


推荐阅读