I’m making a game about delivering orders and I wrote a code that when a certain player delivers a wrong order or an order that is not requested it should enable a certain particle system only on that player (that particle sys is part of the player game object but it is disabled at the start) so it works fine for the clients he delivers a wrong order and it shows the particle sys on the client character you can see it both from the view of the client and the server but when the server/host delivers a wrong order it shows the particle sys on the server/host character only from the side/view of the server/host and the client can’t even see it on the character that belongs to the server/host and it gives an error in the debug of the client it says (NotServerException: Only the server can find player objects from other clients. Unity.Netcode.NetworkSpawnManager.GetPlayerNetworkObject).
This code is what I have tried and it throws the error above:
[ServerRpc(RequireOwnership = false)]
private void NotifyWrongOrderDeliveryServerRpc(ulong playerId)
{
Debug.Log("Server notifying clients about wrong order delivered by player with ID: " + playerId);
RpcClientShowDizzyEffectClientRpc(playerId);
}
[ClientRpc]
private void RpcClientShowDizzyEffectClientRpc(ulong playerId)
{
Debug.Log("Client received notification about wrong order delivered by player with ID: " + playerId);
// Request the player object from the server using the player ID
NetworkObject playerObject = NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(playerId);
if (playerObject != null)
{
Player player = playerObject.GetComponent<Player>();
if (player != null)
{
Debug.Log("Showing Dizzy effect locally for player with ID: " + playerId);
player.StartCoroutine(player.ShowDizzyEffectLocally());
}
else
{
Debug.LogError("Player component not found on the player object.");
}
}
else
{
Debug.LogError("Player object not found for player with ID: " + playerId);
}
}
// Method to deliver wrong order, called when wrong order is detected
private void DeliverWrongOrder(ulong playerId)
{
// Logic to handle wrong order delivery...
Debug.Log("Player with ID " + playerId + " delivered the wrong order.");
// Notify all clients about the wrong order delivery
NotifyWrongOrderDeliveryServerRpc(playerId);
}
// Method to detect wrong order and deliver it
private void DetectAndDeliverWrongOrder()
{
// Logic to detect wrong order and extract the player ID
// For demonstration purposes, let's assume you have access to the player ID here
ulong wrongOrderPlayerId = ExtractPlayerId();
// Call the method to deliver wrong order with the extracted player ID
DeliverWrongOrder(wrongOrderPlayerId);
}
// Method to extract player ID (replace this with your actual logic)
private ulong ExtractPlayerId()
{
// Example: Extract player ID from a networked object
NetworkObject playerObject = GetPlayerObject();
if (playerObject != null)
{
return playerObject.OwnerClientId;
}
else
{
// Handle the case where player object is not found
Debug.LogError("Player object not found.");
return NetworkManager.Singleton.LocalClientId; // Or return a default value
}
}
// Example method to get player object (replace this with your actual logic)
private NetworkObject GetPlayerObject()
{
// Example: Get player object from NetworkSpawnManager
return NetworkManager.Singleton?.SpawnManager?.GetPlayerNetworkObject(NetworkManager.Singleton.LocalClientId);
}