[英]How to use ClientRpc (or ServerRpc) in unity Netcode for GameObject?
我無法理解如何正確使用這些 Rpc。 有時他們工作有時他們不工作。 示例:我在場景中有這個游戲對象(未在連接時實例化),稱為“Book”,它有一個腳本(“SpellManager”),所以當兩個玩家連接時,他們的場景中都有這個 object。 現在在該 SpellManager 腳本中,我希望我的主機/服務器在碰撞時更改拼寫變量。 假設我有一個“string newSpell”變量,如何在服務器端更改此變量然后將信息發送給客戶端? 我試過這樣的事情(簡化):
private string newSpell;
private void OnTriggerEnter(Collider other)
{
if (NetworkManager.Singleton.IsServer)
{
newSpell = "Earth";
testClientRpc(Time.frameCount, newSpell);
}
else
{
print("OnClient ");
}
}
[ClientRpc]
public void testClientRpc(int somenumber, string _newSpell)
{
newSpell = _newSpell;
}
但這會在服務器上引發錯誤: NullReferenceException: Object reference not set to an instance of an object Unity.Netcode.NetworkBehaviour.get_NetworkManager () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.4/Runtime/Core/NetworkBehaviour.cs:219)
它來自顯然是 null 的_newSpell
變量? 我什至嘗試在 ClientRpc 中硬編碼 newspell = "Earth",同樣的錯誤。 一個例子/視頻將不勝感激,我找不到類似的東西
NetworkBehaviour
派生的腳本(因為您正在使用RPC
)必須附加一個NetworkObject
組件。RPC
,則必須在屬性選項中將requireOwnership
選項設置為 false 即[ServerRpc(requireOwnership = false)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.