- Home /
Error: NullReferenceException: Object reference not set to an instance of an object
The basis of the below script is to get my NPC to move from waypoint to waypoint. It is such a simple script but I don't understand why I am receiving the error. Every object is referenced in my hierarchy. :
NullReferenceException: Object reference not set to an instance of an object Basic_AI.Update () (at Assets/Assets/Scripts/Basic_AI.cs:65)
Here are the scripts
using UnityEngine;
using System.Collections;
public class Basic_AI : MonoBehaviour {
// This shows the waypoints
public GameObject[] Waypoint = new GameObject [5];
// This adds a character controller, so the NPC can move through the waypoints.
private CharacterController controller;
//These are AI Conditionss for the different type of units
private bool attackAI;
private bool aiEngage;
private bool flightAI;
private int unitType;
private bool agression;
// This is a counter to determine which waypoints have been acocmplished
private int waypointReached = 0;
// This determines how fast the NPC will turn and move towards a waypoint
private int rotationSpeed = 10;
private int speed;
void Start () {
// This pulls the stistics from the sister script for the NPC.
bool infantryUnit = GetComponent <Stats_Script>().infantryUnit;
bool groundUnit = GetComponent <Stats_Script>().groundUnit;
bool airUnit = GetComponent <Stats_Script>() .airUnit;
speed = GetComponent <Stats_Script>().speed;
// These are conditions set for each NPC.s
if (airUnit == true)
{
unitType = 3;
flightAI = true;
}
else if (groundUnit == true)
{
unitType = 2;
flightAI = false;
aiEngage = true;
}
else if (infantryUnit == true)
{
unitType = 1;
flightAI = false;
aiEngage = true;
}
}
void Update () {
// ---------------------------------- MOVE TO WAYPOINT --------------------------------------------------
if (aiEngage == true)
{
if (gameObject.transform.position != Waypoint[waypointReached].transform.position)
{
gameObject.transform.rotation = Quaternion.Slerp (gameObject.transform.rotation, Quaternion.LookRotation (Waypoint[waypointReached].transform.position - gameObject.transform.position), rotationSpeed * Time.deltaTime);
controller.SimpleMove (transform.forward * speed);
}
// ------------------------- WAYPOINT REACHED CONDITIONS ------------------------------------------------
else if (gameObject.transform.position == Waypoint[waypointReached].transform.position)
{
waypointReached ++;
}
}
}
}
and here is the referenced Stat Script
using UnityEngine;
using System.Collections;
public class Stats_Script : MonoBehaviour {
public bool infantryUnit;
public bool groundUnit;
public bool airUnit;
public bool aggression;
public int life;
public int attack;
public int speed;
public int defense;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (infantryUnit == true)
{
groundUnit = false;
airUnit = false;
}
if (groundUnit == true)
{
infantryUnit= false;
airUnit= false;
}
if (airUnit == true)
{
infantryUnit = false;
groundUnit = false;
}
}
}
Answer by samqweqwe · Jul 29, 2014 at 12:25 AM
The variable controller is never set, i dont think. In the start/Awake function add
controller = GetComponent<CharacterController>();
Hope this helps.
Your answer
Follow this Question
Related Questions
How to use LookAt() to make a gameobject look at another gameobject slowly? 0 Answers
Playmaker Script Issue with Quaternion Lerp speed value 0 Answers
how do i use Quaternion lerp on only the Y and Z axis 0 Answers
Copy rotation of targetObject to current object with an influence in the range from 0-1. 0 Answers
How to rotate towards the mouse on one axis while snapping 0 Answers