- Home /
Array index out of range after build
Hello, In editor works good but after build there is error IndexOutOfRangeException: Index was outside the bounds of the array.
int rand = Random.Range(0, (cars.Length));
vehicle = Instantiate(cars[rand]);
I tried with cars.length-1 too but not working. There is only one element in cars array but will be more but at this time I want to make it works with one element in the array. Why in editor it works but not after build?
Are you sure cars.Length
is greater than 0 in the build? How do you populate the array? Can you:
Add a
Debug.LogError(cars.Length);
before callingRandom.Range
Open your build settings
Check the
Development build
optionBuild your game
See what the console outputs
outputs 0, but why, arrays have prefabs, they do not reset when the game is turned on in editor, why do they reset after build?
Try to move your prefabs into the "Assets/Prefabs" folder. $$anonymous$$aybe they are located in some unexpected folder and as result they do not included into the final build.
Can it happen, that the array is not existing at all? I run into same problems like, you create the variable like this: 'GameObject[] cars', and then the program goes a way where it gets no value at all. since its an array it will let you use the [] call, but throws an exception even for the 0 value.
Answer by Wenon · Jun 15, 2018 at 05:04 PM
Please can anyone help me with arrays which resets after build with no reason?