C#
C#

help

Root Question Message

Zenternion
Zenternion10/16/2022
ArgumentOutOfRangeException

Whenever I run this I get the error:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


        int i = 1;
        List<Object> cityObjects = new List<Object>(); 
        foreach(feature cityData in cities.features)
        {
            if (cityObjects[i] == null)
            {
                CityData tCity = SetCityVars(cityData);

                GameObject citiesObject = new GameObject($"City {i}");
                citiesObject.transform.parent = transform;
                    
                Object cityToUse;
                //float? cityRadius;

                if (tCity.POP_MAX <= 500000 && tCity.POP_MAX > 0) 
                {
                    cityToUse = CityTo500K; 
                }
                else if (tCity.POP_MAX <= 1000000 && tCity.POP_MAX > 500000) 
                {
                    cityToUse = CityTo1M;
                }
                else if (tCity.POP_MAX > 1000000) 
                {
                    cityToUse = CityToRest; 
                }
                else
                {
                    cityToUse = CityTo500K; 
                }
                    
                CoordinateDegrees coordinate = new CoordinateDegrees(tCity.coordinates[0], tCity.coordinates[1]);
                Vector3 cityPosition = coordinate.CoordinateToPoint(coordinate);
                Vector3 cityDirection = citiesObject.transform.position - cityPosition;
                Quaternion cityRotation = Quaternion.LookRotation(cityDirection);

                Object CityMesh = Object.Instantiate(cityToUse, cityPosition, cityRotation, citiesObject.transform);
                cityObjects.Insert(i, CityMesh);
                i += 1;
            }
Becquerel
Becquerel10/16/2022
When you do cityObjects[i] at the start of the foreach loop, cityObjects likely doesn't have anything at element [1], the initial value of i
Becquerel
Becquerel10/16/2022
just checked, and yeah, cityObjects will have a size of 0
Zenternion
Zenternion10/16/2022
yea
Becquerel
Becquerel10/16/2022
so you're going past the bounds of the list
Zenternion
Zenternion10/16/2022
its an empty list to check if the element exists
Zenternion
Zenternion10/16/2022
so
Becquerel
Becquerel10/16/2022
you can still give the list things to initialize it
Becquerel
Becquerel10/16/2022
var x = new List<object>() { null, null, null }' etc
Becquerel
Becquerel10/16/2022
generally i hate using array syntax, though, precisely because it causes issues like this
Becquerel
Becquerel10/16/2022
consider a nested foreach
Zenternion
Zenternion10/16/2022
im not gonna do that 7342 times
Zenternion
Zenternion10/16/2022
but
Zenternion
Zenternion10/16/2022
another way to check if an object already exists
Zenternion
Zenternion10/16/2022
ok
Zenternion
Zenternion10/16/2022
so
Zenternion
Zenternion10/16/2022
I came up with this
Zenternion
Zenternion10/16/2022
Object[] cityCheck = new Object[cities.features.Length];
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy