- Home /
Array index is out of range and Raycast question
I'm trying to make a tower defense game and right now I am working on the simple AI for the enemy.
I want the enemy to, when created, make a ray to each of the waypoints, in some order. And if there is something between the enemy and the waypoint, it will move on and check the next waypoint.
Here is the code I have right now. It is telling me I have an error on line 25, which is in the start function. It is also not going through with the raycast, or if it is, then it is coming up with any results. Meaning, it is neither giving me a message, nor changing the current waypoint.
var speed: float;
var waypointLength: int;
var myPos: Vector3;
var hit: RaycastHit;
var spawnPoint: GameObject;
private var waypointObjects: GameObject[];
private var waypoint: Transform[];
private var curWaypoint: int;
waypointObjects = GameObject.FindGameObjectsWithTag("Waypoint");
waypointLength = waypointObjects.length;
speed = 2;
spawnPoint = GameObject.FindGameObjectWithTag("Spawn");
// - - - - - - - - - -
function Start () {
waypoint = new Transform[waypointLength];
waypoint[0] = spawnPoint.transform;
for (i = 1; i<waypointObjects.length+1; i++) {
waypoint[i] = waypointObjects[i].transform;
}
curWaypoint = 0;
}
// - - - - - - - - - -
function Update () {
transform.rotation = Quaternion.identity;
myPos = transform.position;
if (curWaypoint < waypoint.length) {
var target : Vector3 = waypoint[curWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if (moveDirection.magnitude < 0.5) {
for (i=0;i<waypoint.length;i++) {
Debug.Log("Before For");
if (Physics.Raycast(myPos,myPos-waypoint[i].position,hit,Vector3.Distance(myPos,waypoint[i].position))) {
if (hit.collider.gameObject.tag == "Wall") {
Debug.Log("Wall hit");
} else {
curWaypoint = i;
}
}
}
} else {
velocity = moveDirection.normalized * speed;
}
}
rigidbody.velocity = velocity;
Debug.DrawLine(transform.position,waypoint[curWaypoint].position,Color.green);
}
// - - - - - - - - - -
Also, if anyone could tell me, or at least point me in the right direction, how to make it so it does not go back to any waypoints it has already been to.
Thank you for your help in advance.
If your array has zero elements, array[0] (which, of course, refers to the first element in the array) will be out of range. You'll need to first make sure that the array has elements in it.
Also, you are using 'GameObject.FindWithTag' as a field initialiser. This is dangerous. You should do that in Start, ins$$anonymous$$d.
Thanks for the help. But, my array doesn't have zero elements. At least, I'm pretty sure it doesn't, because in the line above the one where I set the first term, I set the array's length to that of another array's.
"waypointLength = waypointObjects.length;" should also be moved to the Start-function.
Your answer
Follow this Question
Related Questions
Arrays rebels to my power! 1 Answer
OnGui function - Array is out of index question 1 Answer
Array Index out of range? 1 Answer
Index out of range exception - C# - Can't find the problem 1 Answer
Array index is out of Range!? 1 Answer