I have a host and a client, the host is trying to spawn a prefab, and I do that with this method, which is called from a button press
public void CreateChunks () {
if(!IsServer) return;
SpawnerController.Instance.SpawnChunkServerRpc(chunkPrefab);
}
The ServerRpc method looks like this:
[ServerRpc]
public void SpawnChunkServerRpc(HexGridChunk chunkPrefab)
{
//if (chunkPrefab)
{
HexGridChunk chunk = Instantiate(chunkPrefab);
chunk.GetComponent<NetworkObject>().Spawn();
}
}
For some reason, it’s being called twice. I checked with debugging to make sure and the CreateChunks() method is only being called once.
The first time that the SeverRpc method is being called, the chunkPrefab is null, which causes an error with this stacktrace:
NullReferenceException
UnityEngine.Object.Instantiate[T] (T original) (at <a0ef933b1aa54b668801ea864e4204fe>:0)
SpawnerController.SpawnChunkServerRpc (HexGridChunk chunkPrefab) (at Assets/Scripts/SpawnerController.cs:61)
SpawnerController.__rpc_handler_2692164904 (Unity.Netcode.NetworkBehaviour target, Unity.Netcode.FastBufferReader reader, Unity.Netcode.__RpcParams rpcParams) (at <022d067f7ef14d398ee91bdbe33df528>:0)
Unity.Netcode.RpcMessageHelpers.Handle (Unity.Netcode.NetworkContext& context, Unity.Netcode.RpcMetadata& metadata, Unity.Netcode.FastBufferReader& payload, Unity.Netcode.__RpcParams& rpcParams) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.1/Runtime/Messaging/Messages/RpcMessages.cs:77)
Rethrow as Exception: Unhandled RPC exception!
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.RpcMessageHelpers:Handle(NetworkContext&, RpcMetadata&, FastBufferReader&, __RpcParams&) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.1/Runtime/Messaging/Messages/RpcMessages.cs:81)
Unity.Netcode.ServerRpcMessage:Handle(NetworkContext&) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.1/Runtime/Messaging/Messages/RpcMessages.cs:122)
Unity.Netcode.NetworkBehaviour:__endSendServerRpc(FastBufferWriter&, UInt32, ServerRpcParams, RpcDelivery) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.1/Runtime/Core/NetworkBehaviour.cs:93)
SpawnerController:SpawnChunkServerRpc(HexGridChunk) (at Assets/Scripts/SpawnerController.cs:58)
HexGrid:CreateChunks() (at Assets/Scripts/Hex/HexGrid.cs:306)
<>c:<Start>b__8_4() (at Assets/Scripts/NetworkUI.cs:89)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385)
Immediately after that it’s being called again, this time with the prefab not being null, which is then successfully instantiated and spawned for the client as well.
I tried to ignore the first call using an “if” check, however when it isn’t commented out, for some reason there is no 2nd call and the prefab won’t be instantiated and spawned.
If I then click on the button again and again in the same session, there will only be one call, with the prefab not being null and thus the instantiation & spawning will work correctly without any errors except for the very first time the button is clicked.
The two calls also have different callstacks, this is for the first one (with the prefab being null):
SpawnerController.SpawnChunkServerRpc()at F:\Repos\Unity3D\project\Assets\Scripts\SpawnerController.cs:line 59 [2]
SpawnerController.__rpc_handler_2692164904()
RpcMessageHelpers.Handle()at F:\Repos\Unity3D\project\Library\PackageCache\com.unity.netcode.gameobjects@1.0.1\Runtime\Messaging\Messages\RpcMessages.cs:line 77
ServerRpcMessage.Handle()
NetworkBehaviour.__endSendServerRpc()
SpawnerController.SpawnChunkServerRpc() [1]
HexGrid.CreateChunks()
NetworkUI.<>c.<Start>b__8_4()
InvokableCall.Invoke()
UnityEvent.Invoke()
Button.Press()
Button.OnPointerClick()
ExecuteEvents.Execute()
ExecuteEvents.Execute<IPointerClickHandler>()
StandaloneInputModule.ReleaseMouse()
StandaloneInputModule.ProcessMousePress()
StandaloneInputModule.ProcessMouseEvent()
StandaloneInputModule.ProcessMouseEvent()
StandaloneInputModule.Process()
EventSystem.Update()
And this is for the second one (with the prefab being correct):
SpawnerController.SpawnChunkServerRpc()at F:\Repos\Unity3D\project\Assets\Scripts\SpawnerController.cs:line 59
HexGrid.CreateChunks()at F:\Repos\Unity3D\project\Assets\Scripts\Hex\HexGrid.cs:line 306
NetworkUI.<>c.<Start>b__8_4()
InvokableCall.Invoke()
UnityEvent.Invoke()
Button.Press()
Button.OnPointerClick()
ExecuteEvents.Execute()
ExecuteEvents.Execute<IPointerClickHandler>()
StandaloneInputModule.ReleaseMouse()
StandaloneInputModule.ProcessMousePress()
StandaloneInputModule.ProcessMouseEvent()
StandaloneInputModule.ProcessMouseEvent()
StandaloneInputModule.Process()
EventSystem.Update()
I’d like to know why is the ServerRpc being called twice and how can I deal with it to remove the error