Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved ServerRpc method cannot be executed by OnPointerClick

Discussion in 'Netcode for GameObjects' started by Nanachi88, Aug 20, 2023.

  1. Nanachi88

    Nanachi88

    Joined:
    Aug 10, 2022
    Posts:
    14
    I have tested with the debug log.

    • The OnPointerClicks works as intended
    • Network variable that also work on the OnPointerClicks method
    • The script derive from NetworkBehaviour and it has NetworkObject scrip in the same game object
    Problem:
    The server rpc method cannot be called. When I tried without using the rpc stuff, the script work as intended.

    Question :
    Are there special methods in implementing the Unity event system with Netcode for Gameobjects?


    Code (CSharp):
    1. using MIST.Inventory;
    2. using MIST.Item.Model;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using TMPro;
    7. using Unity.Netcode;
    8. using UnityEngine;
    9. using UnityEngine.EventSystems;
    10. using UnityEngine.UI;
    11.  
    12. public class ObjectInventorySlot : NetworkBehaviour, IPointerClickHandler
    13. {
    14.     #region Fields
    15.     [SerializeField] private Image itemIconImage;
    16.     [SerializeField] private TextMeshProUGUI quantityText;
    17.  
    18.     [SerializeField] private GameObject emptyVisual;
    19.     [SerializeField] private GameObject filledVisual;
    20.     #endregion
    21.  
    22.     #region Parameters
    23.     NetworkObjectReference networkRef;
    24.     ObjectInventorySystem objectInventorySystem;
    25.     private ItemModel item;
    26.     ulong clientID;
    27.     #endregion
    28.  
    29.     #region Network
    30.     private NetworkVariable<int> counter = new NetworkVariable<int>();
    31.     #endregion
    32.  
    33.     public override void OnNetworkSpawn()
    34.     {
    35.         if (IsServer)
    36.         {
    37.             counter.Value = 0;
    38.         }
    39.     }
    40.  
    41.     public void UpdateItem(NetworkObjectReference networkRef, ObjectInventorySystem inventory, ItemModel item)
    42.     {
    43.         this.networkRef = networkRef;
    44.         this.item = item;
    45.         objectInventorySystem = inventory;
    46.  
    47.         UpdateObjectInventoryUI();
    48.     }
    49.  
    50.     private void UpdateObjectInventoryUI()
    51.     {
    52.         emptyVisual.SetActive(item == null);
    53.         filledVisual.SetActive(item != null);
    54.         if (item != null)
    55.         {
    56.             //Debug.Log("item.ID = " + item.ID);
    57.             ItemBaseSO itemData = objectInventorySystem.GetItemBaseFromID(item.ID);
    58.             if (itemData != null)
    59.             {
    60.                 itemIconImage.sprite = itemData.GetItemImage();
    61.             }
    62.             quantityText.text = item.Quantity.ToString();
    63.         }
    64.     }
    65.  
    66.     public void OnPointerClick(PointerEventData eventData)
    67.     {
    68.      
    69.         Debug.Log("On Pointer CLick");
    70.         counter.Value++;
    71.         Debug.Log("counter.Value" + counter.Value);
    72.         RequestLootItemServerRpc(networkRef);
    73.     }
    74.  
    75.  
    76.     [ServerRpc]
    77.     public void RequestLootItemServerRpc(NetworkObjectReference networkRef)
    78.     {
    79.         GiveLootClientRpc(networkRef);
    80.     }
    81.  
    82.     [ClientRpc]
    83.     public void GiveLootClientRpc(NetworkObjectReference networkRef)
    84.     {
    85.         // Check if this is the correct client to respond to
    86.         NetworkObject networkObject = networkRef;
    87.         clientID = networkObject.OwnerClientId;
    88.  
    89.         Debug.Log("clientID : " + clientID);
    90.  
    91.         if (NetworkManager.Singleton.LocalClientId != clientID)
    92.             return;
    93.  
    94.         var result = PlayerInventorySystem.Instance.TryAddItem(networkRef,item, item.Quantity);
    95.         bool itemCollected = result.Item1;
    96.         int leftoverItems = result.Item2;
    97.  
    98.         if (!itemCollected) return;
    99.  
    100.         if (leftoverItems > 0)
    101.         {
    102.             item.Quantity = leftoverItems;
    103.             UpdateItem(networkObject, objectInventorySystem, item);
    104.             return;
    105.         }
    106.     }
    107.  
    108.     public void Dispose()
    109.     {
    110.     }
    111.  
    112.     /*
    113.   private void CheckUpdateQuantity(ItemModel item, ObjectInventorySystem inventory)
    114.   {
    115.       if (this.item == item)
    116.       {
    117.           UpdateItem(item, inventory);
    118.       }
    119.   }
    120.   */
    121. }
    122.  
     
    Last edited: Aug 20, 2023
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Was the object spawned before you tried to call the RPC? Also, do:
    [ServerRPC (requireOwnership = false)]

    If you want the clients to be able to call that RPC.
     
  3. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    188
    Hi there,
    I problem seems at this line:

    [ServerRpc]
    public void RequestLootItemServerRpc(NetworkObjectReference networkRef)
    {
    GiveLootClientRpc(networkRef);
    }


    All client can call the "RequestLootItemServerRpc" but, just Host/Server call the "GiveLootClientRpc(networkRef);".
    This means "GiveLootClientRPC" runs only on the server/Host side. Because "if (NetworkManager.Singleton.LocalClientId != clientID)" line only passes on Host/Server side not on others client's side.
     
  4. Nanachi88

    Nanachi88

    Joined:
    Aug 10, 2022
    Posts:
    14
    Hello thank you for replying, the reason I did not add
    [ServerRPC (requireOwnership = false)]

    because I forgot, and normally I test with Server first. I have implemented this, but still, OnClick, either the ServerRpc or ClientRpc method cannot be executed.

    It is very frustrating, even testing with empty method with only Debug.Log, it doesnt work.
     
  5. Nanachi88

    Nanachi88

    Joined:
    Aug 10, 2022
    Posts:
    14
    I have implemented [ServerRPC (requireOwnership = false)] . Nevertheless, neither Server or Client can execute ServerRpc nor ClientRpc OnClick.

    I have tried OnPointerDown as well, it just doesnt work. I have tried using Button, doesnt work.
     
  6. Nanachi88

    Nanachi88

    Joined:
    Aug 10, 2022
    Posts:
    14
    I have tested this code below, it works. I will try to review and try another solution again related to my code


    Code (CSharp):
    1. public class testOnclick : NetworkBehaviour, IPointerClickHandler
    2. {
    3.     public void OnPointerClick(PointerEventData eventData)
    4.     {
    5.         Debug.Log("test 1");
    6.         testServerRpc();
    7.     }
    8.  
    9.     [ServerRpc]
    10.     private void testServerRpc()
    11.     {
    12.         Debug.Log("test 2");
    13.     }
    14.  
    15.  
    16. }
     
  7. Nanachi88

    Nanachi88

    Joined:
    Aug 10, 2022
    Posts:
    14
    The problem has been solved. I deactivated the game object in which this script is attached. I deactivated it in Awake() meaning that the network stuff could not initialize properly.