Question by
DARTHMAYDAR11 · Feb 01, 2016 at 07:00 PM ·
scripting problemnullreferenceexception
This script was functioning perfectly then i turned it back on and it is giving me a null reference error
using UnityEngine; using System.Collections;
public class Zombie_controller : MonoBehaviour {
NavMeshAgent zombie;
Animator anim;
AudioSource aud;
EnemyHealth enemyhealth;
public float timeBetweenAttacks = .5f;
public int attackDamage = 10;
GameObject Player;
GameObject BO;
GameObject BO1;
GameObject BO2;
GameObject BO3;
GameObject BO4;
GameObject BO5;
setGameboard set;
WoodenBoardHealth HealthWood;
WoodenBoardHealth HealthWood1;
WoodenBoardHealth HealthWood2;
WoodenBoardHealth HealthWood3;
WoodenBoardHealth HealthWood4;
WoodenBoardHealth HealthWood5;
bool playerInRange;
bool boardInRange;
bool boardInRange1;
bool boardInRange2;
bool boardInRange3;
bool boardInRange4;
bool boardInRange5;
playerHealth PlayerHealth;
public Transform thePoint;
public float timer;
void Start () {
BO = GameObject.FindGameObjectWithTag ("WoodenBoard");
BO1 = GameObject.FindGameObjectWithTag ("WoodenBoard1");
BO2 = GameObject.FindGameObjectWithTag ("WoodenBoard2");
BO3 = GameObject.FindGameObjectWithTag ("Wooden Board3");
BO4 = GameObject.FindGameObjectWithTag ("Wooden Board4");
BO5 = GameObject.FindGameObjectWithTag ("Wooden Board5");
HealthWood = BO.GetComponent<WoodenBoardHealth> ();
HealthWood1 = BO1.GetComponent<WoodenBoardHealth> ();
HealthWood2 = BO2.GetComponent<WoodenBoardHealth> ();
HealthWood3 = BO3.GetComponent<WoodenBoardHealth> ();
HealthWood4 = BO4.GetComponent<WoodenBoardHealth> ();
HealthWood5 = BO5.GetComponent<WoodenBoardHealth> ();
Player = GameObject.FindGameObjectWithTag ("Player");
PlayerHealth = Player.GetComponent <playerHealth> ();
aud = GetComponent<AudioSource> ();
anim = GetComponent<Animator> ();
enemyhealth = GetComponent<EnemyHealth> ();
set = GetComponent<setGameboard> ();
zombie = GetComponent<NavMeshAgent> ();
}
void Update () {
timer += Time.deltaTime;
zombie.SetDestination (thePoint.position);
if (timer >= timeBetweenAttacks && playerInRange) {
Attack ();
}
if (timer >= timeBetweenAttacks && boardInRange ) {
SmashWood ();
HealthWood.Smashing.Play ();
}
if (timer >= timeBetweenAttacks && boardInRange1) {
SmashWood1 ();
HealthWood1.Smashing.Play ();
}
if (timer >= timeBetweenAttacks && boardInRange2) {
SmashWood2 ();
HealthWood2.Smashing.Play ();
}
if (timer >= timeBetweenAttacks && boardInRange3) {
SmashWood3 ();
HealthWood3.Smashing.Play ();
}
if (timer >= timeBetweenAttacks && boardInRange4) {
SmashWood4 ();
HealthWood4.Smashing.Play ();
}
if (timer >= timeBetweenAttacks && boardInRange5) {
SmashWood5 ();
HealthWood5.Smashing.Play ();
}
if (HealthWood.ISsmashed == true) {
zombie.enabled = true;
}
if (HealthWood1.ISsmashed == true) {
zombie.enabled = true;
}
if (HealthWood2.ISsmashed == true) {
zombie.enabled = true;
}
if (HealthWood3.ISsmashed == true) {
zombie.enabled = true;
}
if (HealthWood4.ISsmashed == true) {
zombie.enabled = true;
}
if (HealthWood5.ISsmashed == true) {
zombie.enabled = true;
}
playerInRange = false;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Player")) {
playerInRange = true;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if(other.gameObject.CompareTag ("WoodenBoard")){
boardInRange = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if(other.gameObject.CompareTag ("WoodenBoard1")){
boardInRange1 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if(other.gameObject.CompareTag ("WoodenBoard2")){
boardInRange2 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if(other.gameObject.CompareTag ("Wooden Board3")){
boardInRange3 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if(other.gameObject.CompareTag ("Wooden Board4")){
boardInRange4 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if(other.gameObject.CompareTag ("Wooden Board5")){
boardInRange5 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
}
void OnTriggerStay (Collider other)
{
if (other.gameObject.CompareTag ("Player")) {
playerInRange = true;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if (other.gameObject.CompareTag ("WoodenBoard")) {
boardInRange = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if (other.gameObject.CompareTag ("WoodenBoard1")) {
boardInRange1 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if (other.gameObject.CompareTag ("WoodenBoard2")) {
boardInRange2 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if (other.gameObject.CompareTag ("Wooden Board3")) {
boardInRange3 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if (other.gameObject.CompareTag ("Wooden Board4")) {
boardInRange4 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
if (other.gameObject.CompareTag ("Wooden Board5")) {
boardInRange5 = true;
zombie.enabled = false;
anim.SetTrigger ("Attack");
anim.Play ("zombie_attack");
}
}
void Attack ()
{
timer = 0f;
if(PlayerHealth.currentHealth > 0)
{
PlayerHealth.TakeDamage (attackDamage);
}
}
void SmashWood (){
timer = 0f;
if (HealthWood.CurrHealth > 0) {
HealthWood.FuckingHit (attackDamage);
}
}
void SmashWood1(){
timer = 0f;
if (HealthWood1.CurrHealth > 0) {
HealthWood1.FuckingHit (attackDamage);
}
}
void SmashWood2(){
timer = 0f;
if (HealthWood2.CurrHealth > 0) {
HealthWood2.FuckingHit (attackDamage);
}
}
void SmashWood3(){
timer = 0f;
if (HealthWood3.CurrHealth > 0) {
HealthWood3.FuckingHit (attackDamage);
}
}
void SmashWood4(){
timer = 0f;
if (HealthWood4.CurrHealth > 0) {
HealthWood4.FuckingHit (attackDamage);
}
}
void SmashWood5(){
timer = 0f;
if (HealthWood5.CurrHealth > 0) {
HealthWood5.FuckingHit (attackDamage);
}
}
}
So my problem occurs at line 78 where i set the destination for the NavMeshAgent. As you can see I'm using a public transform so i insert the target in the inspector, but then it tells me there's a null reference please help.
Comment
Does the object that has this script attached (the zombie?) have a Nav$$anonymous$$eshAgent component as well?
Best Answer
Answer by DARTHMAYDAR11 · Feb 02, 2016 at 07:50 AM
So I updated Unity to the current version assembled my zombie again added the same script renamed the transform, and it worked so mystery solved. Very strange that it happened in the first place.