I need to call a ServerRPC method from a player script that is in another class. Here is an example of my code:
DashT.cs
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class DashT : NetworkBehaviour
{
[ServerRpc]
public void UseServerRpc()
{
Debug.Log(1);
}
}
TestUseScript.cs
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class TestUseScript : NetworkBehaviour
{
private void Start()
{
GameObject obj = new GameObject();
obj.name = string.Format("_{0}", typeof(DashT).Name);
DashT skill = obj.AddComponent<DashT>();
if (IsOwner)
skill.UseServerRpc();
}
}
I thought it would work but I got an error:
KeyNotFoundException: The given key 'DashT' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <20deccf348554c238921b06fec015ff8>:0)
Unity.Netcode.NetworkBehaviour.__endSendServerRpc (Unity.Netcode.FastBufferWriter& bufferWriter, System.UInt32 rpcMethodId, Unity.Netcode.ServerRpcParams serverRpcParams, Unity.Netcode.RpcDelivery rpcDelivery) (at ./Library/PackageCache/[email protected]/Runtime/Core/NetworkBehaviour.cs:127)
DashT.UseServerRpc () (at Assets/Scripts/Skills/DashT.cs:11)
TestUseScript.Start () (at Assets/Scripts/Player/TestUseScript.cs:15)
How to fix it?