Hello all,
Been asking questions on how to make a list correctly and this is the script, the issue is im getting a null reference error and my attempts to fix it are failing. So I am requesting the assistance of the community to help me figure out this final issue im having. Thanks in advance, any information is greatly appreciated.
The error:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
waypointnew.Update () (at Assets/Scripts/waypointnew.js:17)
This is pointing to the first line in the function update.
Here is the code I’m working with. Yet again, any help is greatly appreciated:
import System.Collections.Generic;
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
var player : Transform;
private var waypoint = new List.<Transform>();
function Start() {
waypoint.Add(GameObject.Find("wp1").transform);
waypoint.Add(GameObject.Find("wp2").transform);
waypoint.Add(GameObject.Find("wp3").transform);
waypoint.Add(GameObject.Find("wp4").transform);
}
function Update () {
if(currentWaypoint < waypoint.length){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var distFromPlayer : Vector3 = player.position - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 100){
currentWaypoint++;
}
else if(distFromPlayer. magnitude < 200){
velocity = Vector3.zero;
target = player.position;
velocity = (player.position - transform.position).normalized * speed;
if((player.position - waypoint[currentWaypoint].position).magnitude > 250){
target = waypoint[currentWaypoint].position;
velocity = moveDirection.normalized * speed;
}
if(distFromPlayer.magnitude < 200){
velocity = Vector3.zero;
}
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}