- Home /
Runtime IndexOutOfRangeException when testing on iPhone
I have a script that fetches JSON data from a REST api, and translates it using "JsonUtility.FromJson". The scripts is the following:
UnityWebRequest www = UnityWebRequest.Get("http://my_url/getNearPlanes?longitude="+myLocation.y+"&latitude="+myLocation.x+"&altitude="+myLocation.z+"&maxDistance=300");
yield return www.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
MyPlanes planeList = JsonUtility.FromJson<MyPlanes>(www.downloadHandler.text);
for (int i = 0; i < planeList.planes.Count; i++) {
MyPlane mp = planeList.planes [i];
float tempLat = mp.latitude - myLocation.x;
float tempLong = mp.longitude - myLocation.y;
float tempAlt = mp.altitude - myLocation.z;
Instantiate(airPlane, new Vector3 (tempLat,tempAlt,tempLong), Quaternion.identity, this.transform);
}
}
I have a MyPlanes object:
[Serializable]
public class MyPlanes{
public List<MyPlane> planes;
}
and a MyPlane struct:
[Serializable]
public struct MyPlane{
public string icao24;
public string callsign;
public string origin_country;
public float time_position;
public float time_velocity;
public float longitude;
public float latitude;
public float altitude;
public bool on_ground;
public float velocity;
public float heading;
public float vertical_rate;
public int[] sensors;
}
When running this code within Unity it all works fine. The problem is that, when I build for iOS Xcode gives me the following error:
IndexOutOfRangeException: Array index is out of range.
at System.Collections.Generic.List`1[T].get_Item (Int32 index) [0x00000] in <filename unknown>:0
at AirPlanesController+<Start>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in <filename unknown>:0
(Filename: currently not available on il2cpp Line: -1)
When I write print(planeList.planes.Count) I get the correct number of elements (list is not empty). The error seems to occur when I want to fetch a specific element in the list: planeList.planes[i].
I guess the issue relates to something around the struct type. Any suggestions?
Thanks in advance.
Answer by lassevestergaard · Oct 25, 2016 at 08:09 AM
I have now done some further investigations, and it seems to work now. I have no real idea why, but what I did is essentially only replacing public List<MyPlane> planes;
with public MyPlane[] planes;
in the MyPlanes class.
I also changed MyPlane to class instead of struct (it didn't make any difference, but I just didn't really understand the need for a struct here).