Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved ServerRPC Behavior when Using Coroutines.

Discussion in 'Netcode for GameObjects' started by Sushi8819, Oct 31, 2023.

  1. Sushi8819


    Oct 20, 2023
    I want to perform an HTTP request from a ServerRpc and store the result in a NetworkList.
    The code is simplified as follows.
    The issue is that the NetworkList is updated when the ServerRpc is called for the second time. This means that the result of the first call is updated at the time of the second ServerRpc call, and the result of the second call is updated at the time of the third ServerRpc call, and so on.
    The same behavior is observed with NetworkList's OnValueChanged event.
    If anyone knows the cause of this, I would appreciate your help.

    Code (CSharp):
    2. public NetworkList<UserInfo> users;
    4. OnEnable(){
    5.     users.OnListChanged += NetworkListChangedListener;
    6. }
    8. private void NetworkListChangedListener(NetworkListEvent<UserInfo> changeEvent)
    9.     {
    10.     Debug.Log("NetworkList is changed.")
    11.     }
    13. [ServerRpc(RequireOwnership = false)]
    14. private void SetUserInfoServerRPC(string id, ServerRpcParams serverRpcParams = default)
    15.     {
    16.         Debug.Log("ServerRPC");
    17.         StartCoroutine(GetUserInfo(id, serverRpcParams.Receive.SenderClientId));
    18.     }
    20. private IEnumerator GetUserInfo(string uToken, ulong clientid)
    21. {
    22.     ....
    23.     yield return request.SendWebRequest();
    24.     ....
    26.     UserInfo userinfo = new UserInfo(infoA, infoB, infoC);
    27.     users.Add(userinfo);
    28. }