In the code below, I’ve started a server, and a client is already connected. I created a FirstSpawnObject on the server using the InstantiateAndSpawn method, and the FirstSpawnObject will be in OnNetworkSpawn, and I will also create a SecondSpawnObj with InstantiateAndSpawn.
#nullable enable
using Unity.Netcode;
using UnityEngine;
public class FirstSpawnObj : NetworkBehaviour
{
[SerializeField] private GameObject? networkObject;
public override void OnNetworkSpawn()
{
if (IsServer)
{
FirstSpawnObj_EveryoneRpc("Start");
NetworkObject.InstantiateAndSpawn(networkObject, NetworkManager);
FirstSpawnObj_EveryoneRpc("End");
}
}
[Rpc(SendTo.Everyone)]
public void FirstSpawnObj_EveryoneRpc(string str) =>
print($"{nameof(FirstSpawnObj)}{{{str}}}{{{NetworkObjectId}}}: {nameof(FirstSpawnObj_EveryoneRpc)}");
}
#nullable enable
using Unity.Netcode;
public class SecondSpawnObj : NetworkBehaviour
{
public override void OnNetworkSpawn()
{
if (IsServer) SecondSpawnObj_EveryoneRpc();
}
[Rpc(SendTo.Everyone)]
public void SecondSpawnObj_EveryoneRpc()
{
print($"{nameof(SecondSpawnObj)}{{{NetworkObjectId}}}: {nameof(SecondSpawnObj_EveryoneRpc)}");
}
}
On the server side, all EveryoneRpc will be executed in the normal order, as shown below
However, there is a problem with the order in which the EveryoneRpc is executed on the client side, which will execute the SecondSpawnObj of Everyone first
Rpc, and then execute the EveryoneRpc of the FirstSpawnObject. Causes timing errors!
After I looked at the source code of the Netcode, I guessed that it might be because the SpawnNetworkObjectLocallyCommon method of the NetworkSpawnManager executes OnNetworkSpawn first, and then sends the client to create the Network
Object, which causes the SecondSpawnObj to be more important on the client than the FirstSpawnObject
However, such a running logic can lead to a lot of logic errors in the timing, whether it is the sending of Rpc or the index of a NetworkObject based on ulong. So I would like to ask if this bug can be fixed?