Search Unity

Third Party photon argumentoutofrangeexception empty list

Discussion in 'Multiplayer' started by NatalieBaldwin, Mar 22, 2017.

  1. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    My masterclient instantiates a unit and fills its list<transform> with a few waypoints for navmeshagent walking. Any unit spawned with this method knows it is an AI because playerID is set to "none". If playerID is "none" it automatically calls the function to walk. The function to walk starts with if masterclient or if ismine (same thing same error.) Despite this, the other client spams error messages when the unit instantiates. With google searches i've determined that it's because my list is empty. This seems to be confirmed because if i put an item into the list in the inspector while the game is running then the error stops. The contents of the list should only matter to the masterclient, and the units do walk properly on both clients. But i get a million errors per frame that read:

    ArgumentOutOfRangeException: Argument is out of range.
    Parameter name: index
    System.Collections.Generic.List`1[UnityEngine.Transform].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
    UnitCapabilities.Update () (at Assets/Scripts/UnitCapabilities.cs:110)

    Code (CSharp):
    1. void SpawnCreep () {
    2.         if (PhotonNetwork.isMasterClient == true) {
    3.             GameObject newCreep = PhotonNetwork.InstantiateSceneObject ("Creep", transform.position, Quaternion.identity, 0, null) as GameObject;
    4.             newCreep.name = "Wubaluba";
    5.             newCreep.GetComponent<UnitCapabilities> ().playerID = "none";
    6.             foreach (Transform waypoint in waypoints) {
    7.                 newCreep.GetComponent<UnitCapabilities> ().points.Add (waypoint);
    8.                 //newCreep.GetComponent<UnitCapabilities> ().photonView.RPC("AddPoint", PhotonTargets.All, waypoint);
    9.             }
    the commented section won't work because apparently you can't send transforms through rpc.

    Code (CSharp):
    1.     void Start () {
    2.         if (playerID == "none") {
    3.             if (PhotonNetwork.isMasterClient)
    4.                 GoToNextPoint ();
    5.         }
    6.     }
    7.  
    8.     void GoToNextPoint() {
    9.         if (this.photonView.isMine) {
    10.             if (points.Count == 0)
    11.                 return;
    12.             Debug.Log ("gotonextpoint");
    13.             if (Vector3.Distance (points [0].position, transform.position) > 2f) {
    14.                 agent.destination = points [0].position;
    15.             }
    16.         }
    17.     }
    The debug log "gotonextpoint" is not called on the 2nd client, but the 2nd client is spamming errors. So confused pls help.
     
  2. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    To confirm, line 110 of UnitCapabilities.cs is the one inside GoToNextPoint() checking the distance between the first point in the list and the transform position, right? If not, then your problem is not where you think it is.

    I'm not very familiar with Photon, so there might be additional nuances about when and how it propagates stuff across the network that I am not aware of, but it is entirely conceivable that you're encountering one of those.

    In general, this is the sort of problem that would call for use of the debugger, to hunt down exactly where and why the exception is being thrown. Use of Debug.Log() is a rather low-quality tool for debugging; it has its uses (I use it myself more than I probably should), but it tends to be inefficient at finding the root cause of a problem. So if you're not already familiar with the process of setting breakpoints in your code and stepping over code one line at a time, examining the values of variables as you go, I strongly recommend taking the time to learn about this stuff; its an incredibly valuable toolset.
     
    NatalieBaldwin likes this.
  3. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    False alarm, it took me a full freaking week to notice a spot where all clients were trying to access the List that was causing the error. Thank you as usual Andy :p My script has gotten quite large by now... more advanced debug tools are definitely a requirement.