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

out of index with disconnect function

Discussion in 'Unity Transport' started by ElegantUniverse, Mar 26, 2022.

  1. ElegantUniverse

    ElegantUniverse

    Joined:
    Sep 13, 2018
    Posts:
    78
    Hello
    I have a problem with disconnecting connections once I try to remove the disconnected connection from the NetworkConnection list or native NetworkConnection list I encounter with the Out of index error in the server side .
    this is my code.

    Code (CSharp):
    1.  
    2.     if (_connections.Count > 0)
    3.         {
    4.             for (int i = 0; i < _connections.Count; i++)
    5.             {
    6.  
    7.                 DataStreamReader ServerStream;
    8.                 NetworkEvent.Type cmd;
    9.                 while ((cmd = _serverDrive.PopEventForConnection(_connections[i], out ServerStream))
    10.                     != NetworkEvent.Type.Empty)
    11.                 {
    12.                     if (cmd == NetworkEvent.Type.Connect)
    13.                     {
    14.  
    15.                     }
    16.                     else if (cmd == NetworkEvent.Type.Data)
    17.                     {
    18.  
    19.                
    20.                     }
    21.                     else if (cmd == NetworkEvent.Type.Disconnect)/////////
    22.                     {
    23.  
    24.      
    25.  
    26.                         _connections.RemoveAt(i);
    27.                         //   i--;
    28.  
    29.                         if (i > _connections.Count) break;
    30.  
    31.                     }
    32.  
    33.                 }
    34.             }

    when I remove the " _connections.RemoveAt(i);" the error will not display.

    https://ibb.co/WcM47c0


    Code (CSharp):
    1. ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    2. Parameter name: index
    3.   at System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00029] in <695d1cc93cca45069c528c15c9fdd749>:0
    4.   at System.ThrowHelper.ThrowArgumentOutOfRangeException () [0x00000] in <695d1cc93cca45069c528c15c9fdd749>:0
    5.   at ServerCode.Update () [0x001eb] in <cfbe4c1cbbe34153a2dc6d0d3c1177cb>:0
     
    Last edited: Mar 26, 2022
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Seems to me the
    i > _connections.Count
    is wrong. Should be greater than or equal.

    But really if you're going to remove things from a list as you're iterating through it, you should iterate through it backwards. Otherwise you'll skip entries when removing items (unless you decrease the index after removal, but that just makes the code harder to understand).