Raycasting and Waypoints

I’ve searched the forum and found nothing similar enough to help me out.
I was throwing together a waypoint system in which the waypoints would (on game start) find each other and in the event there was nothing directly between each other, store information in an array.

The problem is that when I included raycasting, I get some errors I dont quite understand.

This is my code that I attach to each waypoint GameObject:

#pragma strict
private var target_waypoint:GameObject;
private var way_array:GameObject[] = new GameObject[6];
private var way_distance:float[] = new float[6];


function Start () {
	GenerateWayMap();
	Debug.Log(this.name);
}

function Update () {
	
	for(wayp in way_array){
		Debug.DrawLine (transform.position, wayp.transform.position, Color.cyan);

	}
	
}

function GenerateWayMap(){
	var wayarray = GameObject.FindGameObjectsWithTag("WayPoint");
	var distance:float =100;
	for(var i=0;i<wayarray.length;i++){
		var wayp:GameObject=wayarray*;*
  •  var dist = Vector3.Distance(wayp.transform.position, transform.position);*
    
  •  var hit : RaycastHit;	*
    
  •  if(dist>0){*
    
  •  if (Physics.Raycast (transform.position, (wayp.transform.position-transform.position).normalized, hit, 2000) ) {*
    
  •  	if(hit.collider.name=="Waypoint"){*
    

way_array*=wayp;
way_distance=dist;*

* }*

* }*
* }*

* }*

}
When I remove the raycasting part of it:
function GenerateWayMap(){
* var wayarray = GameObject.FindGameObjectsWithTag(“WayPoint”);*
* var distance:float =100;*
* for(var i=0;i<wayarray.length;i++){*
_ var wayp:GameObject=wayarray*;
var dist = Vector3.Distance(wayp.transform.position, transform.position);
var hit : RaycastHit; _

way_array=wayp;
way_distance=dist;
_ /
* if(dist>0){*_

* if (Physics.Raycast (transform.position, (wayp.transform.position-transform.position).normalized, hit, 2000) ) {*

* if(hit.collider.name==“Waypoint”){*
way_array*=wayp;
way_distance=dist;*

* }*

* }*
* }*
_ */_

* }*

}
It performs as expected. I get these errors when included raycast:
> IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
Waypoints.GenerateWayMap () (at Assets/Scripts/Waypoints.js:36)
Waypoints.Start () (at Assets/Scripts/Waypoints.js:8)
and
> NullReferenceException: Object reference not set to an instance of an object
Waypoints.Update () (at Assets/Scripts/Waypoints.js:15)
Any help would be greatly appreciated, thanks ahead of time.
OH! And there are 7 "Waypoint"s , which is why I set the array size to 6, so that shouldnt be causing the error.

You have seven waypoints but your arrays are set to 6.

The index starts from 0 to 6 but you need to tell you want 7 objects:

private var way_array:GameObject[] = new GameObject[7];
private var way_distance:float[] = new float[7];

The last one has index 6 but the arrays are now 7 objects.

Ok, my guess is this. The index out of range i explained in my comment below the org question and most likely solves that issue. As for the null ref you should check that your array has been populated before looping through it.

function Update()
{
    if(way_array.length>0)
    {
        for(wayp in way_array)
        Debug.DrawLine (transform.position, wayp.transform.position, Color.cyan);
    }
}

Hey thanks! It works perfectly with your fix, but when waiting for “moderator” approval I was fiddling around-

Making my two initialized variables of type “Array()” and then "Push()"ing the data to them also fixed it.

Thanks so much :smiley: