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.