- Home /
Question by
Empu · Nov 27, 2012 at 01:19 PM ·
arrayrandom.rangeindexoutofrangeexceptionphysics.overlapsphere
IndexOutOfRangeException
I've been getting the IndexOutOfRangeException error on occasions which I can't seem to predict. The script will run fine after several times running but then out of nowhere it will throw this error out. "IndexOutOfRangeException: Array index is out of range."
var platformColliders : Collider[] = Physics.OverlapSphere(transform.position,3, layerPlatform);
var platform : GameObject = platformColliders[Random.Range(0,platformColliders.Length)].gameObject;
Comment
I guess you have to check first if there is at least one element in platformColliders.
if(platformColliders != null && platformColliders.Length != 0)
Best Answer
Answer by Paulius-Liekis · Nov 27, 2012 at 02:10 PM
Check if platformColliders has at least one element.
Your answer
Follow this Question
Related Questions
Creating a record of values generated with Random.Range 1 Answer
ArrayIsOutOfRange 0 Answers
Index Array is out of Range 1 Answer
Array out of index? 1 Answer
IndexOutOfRangeException only on iOS! Android / PC is ok!! 1 Answer