This question was
closed May 08, 2017 at 02:26 PM by
TaliOS for the following reason:
The question is answered, right answer was accepted
Question by
TaliOS · May 08, 2017 at 01:36 PM ·
array-out-of-range-except
Index OutOfRangeException with index being zero ?
I have a problem with an array and I can't figure what's wrong... Unity tells me "Array index is out of range" at line 16 :
public class MovingPlatform : MonoBehaviour {
public float speed = 3f;
Transform[] waypoints;
int currentDestinationIndex = 0;
void Awake () {
waypoints = new Transform[transform.childCount];
for (int i = 0; i < waypoints.Length; i++) {
waypoints [i] = transform.GetChild (i);
}
}
void Update () {
if (transform.position != waypoints[currentDestinationIndex].position) {
transform.Translate ((waypoints [currentDestinationIndex].position - transform.position) * speed * Time.deltaTime);
} else {
if (currentDestinationIndex == waypoints.Length - 1) {
currentDestinationIndex = 0;
} else {
currentDestinationIndex++;
}
}
}
}
The strange thing is that if I advance frame by frame while testing, it still works... Any idea about what's happening ?
Comment