c# - 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?
解决方案
推荐阅读
- java - 使用休眠连接两个表
- android - Room Android 中的一次性请求单结果
- javascript - Summernote 无法在“节点”上执行“appendChild”
- c - 分配二维数组
- android - Espresso:如何在用户同意的情况下对 Sms Retriever 进行 UI 测试?
- css - 有没有办法使用变换比例使内容自动适应父 div?
- r - 删除空白值而不是 R 中的整个列/行
- azure-devops - Azure DevOps 管道无法在运行时扩展任何环境变量
- apache-camel - 使用 Camel Endpoint DSL 和 @EndpointInject 创建不同的端点
- python - Python Pandas DataFrame:意外取消排序多索引