unity3d Unity MLAPI服务器RPC未被调用

7gcisfzg  于 2022-11-25  发布在  其他
关注(0)|答案(6)|浏览(197)

我正在实现一个系统,玩家可以从游戏世界中拾取物品。拾取物品后,我想销毁游戏对象。为此,我想我应该创建一个删除对象的ServerRPC,但ServerRPC没有被调用。我也尝试创建一个ClientRPC,但是那个也没有被调用。我注意到只有当游戏的主持人试图捡起物品时才调用它--有什么解决这个问题的方法吗?documentation是相当光秃秃的,但请让我知道,如果我错过了什么。下面是我的代码:

public override void Interact(GameObject player)
{
    player.GetComponent<PlayerInventory>().AddItem(this.item);
    Debug.Log("before");
    TestClientRpc();
    TestServerRpc();
}

[ServerRpc]
public void TestServerRpc()
{
    Debug.Log("server");
    // Destroy(gameObject);
}

[ClientRpc]
public void TestClientRpc()
{
    Debug.Log("client");
    // Destroy(gameObject);
}

编辑:image of the components on the weapon
编辑:这个类扩展了一个类,这个类扩展了networkbehavior类。对象只是在开始的时候停留在场景中--它们没有被衍生/示例化(不确定这是否重要)。

erhoui1w

erhoui1w1#

对于这个问题最合理的解决方案,要么是前面几行中出现了错误,要么是像其他人指出的那样,requireOwnership没有设置为false。

ymdaylpp

ymdaylpp2#

我有同样的问题。已经看了2天了,但老实说,我倾向于认为这是一个错误。

t98cgbkg

t98cgbkg3#

我需要将以下属性添加到ServerRpc:
[ServerRpc(RequireOwnership = false)]
因为玩家不是物品的所有者。

gmol1639

gmol16394#

我只是花了4个多小时把头撞到墙上,因为他们没有在文档中写这个重要的信息。当从一个不拥有网络对象的客户端调用ServerRpc时,RPC属性应该设置为RequireOwnership = false。

dba5bblo

dba5bblo5#

对我来说,问题是在游戏开始之前,GameObject(带有NetwrokObject组件)就在场景中,我不能使用NetworkVariables和ServerRpc。我认为这些只能在衍生对象上使用,但不确定。所以我会说:示例化对象并将其添加到NetworkManager或在这些对象上使用普通变量。

8yoxcaq7

8yoxcaq76#

有同样的问题,但后来意识到我只是忘了继承NetworkBehaviour
例如:

using Unity.Netcode;
using UnityEngine;
class myObject : NetworkBehaviour {
    public override void Interact(GameObject player)
    {
        player.GetComponent<PlayerInventory>().AddItem(this.item);
        Debug.Log("before");
        TestClientRpc();
        TestServerRpc();
    }

    [ServerRpc]
    public void TestServerRpc()
    {
        Debug.Log("server");
         //Destroy(gameObject);
    }

    [ClientRpc]
    public void TestClientRpc()
    {
        Debug.Log("client");
        // Destroy(gameObject);
    }
}

相关问题