- Home /
Why is my Array going out of bounds??
using UnityEngine;
using System.Collections;
public class XY_MoveScript1 : MonoBehaviour
{
//Movement Parameters.
public Transform[] points;
public bool startAutomaticly;
public bool startFromTrigger;
public bool enableMultiPoints;
public GameObject triggerObject;
public Vector3 offset;
public float speed;
public float delayStartBy = 0;
private bool go;
private Transform destination;
private bool yes;
public bool switchIt = false;
int currentPoint = 0;
void Awake() { transform.position = points[0].position; }
void Start()
{
//---------------Through Errors if Needed----------------
if (!startFromTrigger && !startAutomaticly) { Debug.LogError("You have not specified to StartAutomaticly or StartFromTrigger!" + this.gameObject); return; }
if (startAutomaticly && startFromTrigger) { Debug.LogError("You have StartFromTrigger And Start automaticly checked on " + this.gameObject + "!"); return; }
if (startFromTrigger && !triggerObject) { Debug.LogError("You have Start from trigger checked, but you forgot to apply the TriggerGO1 prefab!" + this.gameObject); return; }
//-------------------------------------------------------
if (points.Length > 2) { enableMultiPoints = true; }
destination = points[currentPoint];
if (startAutomaticly)
{
StartCoroutine(WaitTime());
}
else if (startFromTrigger)
{
go= false;
GameObject trigger = Instantiate(triggerObject, transform.position + offset, transform.rotation) as GameObject;
}
}
void Update()
{
destination = points[currentPoint];
//So currentPoints isn't greater than points length. Or less than 0.
if (currentPoint >= points.Length) { currentPoint = points.Length; }
if(currentPoint <= 0) {currentPoint = 0;}
if (go && enableMultiPoints)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, destination.position, step);
if (currentPoint == points.Length) { switchIt = true; }
if (transform.position == destination.position && !switchIt)
{
currentPoint++;
}
else if (transform.position == destination.position && switchIt)
{
currentPoint--;
}
}
else if (go && !enableMultiPoints)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, destination.position, step);
if (transform.position == points[1].position)
{
destination = points[0];
}
else if (transform.position == points[0].position)
{
destination = points[1];
}
}
}
private IEnumerator WaitTime()
{
//Wait this long.
yield return new WaitForSeconds(delayStartBy);
go = true;
}
}
It's saying Array is out of bounds when it reaches the last destination. Since there are 4 points when it gets to points[3] it says array is out of bounds ins$$anonymous$$d of "switchIt" being true, and then going backwards through the array.
Answer by kacyesp · Sep 03, 2014 at 02:17 AM
At this line:
if (currentPoint == points.Length) { switchIt = true; }
switchIt does become true, which is fine, but in the following else if statement
else if (transform.position == destination.position && switchIt)
in the case that transform.position does not equal destination.position, currentPoint will not be decremented which is why you're going out of bounds.
Awesome, I am very tired, been up way to long. lol Now to figure out how to fix this issue
Your answer
Follow this Question
Related Questions
Find the average of 10 Vectors 4 Answers
Array "size" question. 1 Answer
JavaScript String Question 2 Answers
Instantiating random prefabs 1 Answer
Different timer in a list of gameobject 2 Answers