Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Taxen0 · Feb 01, 2016 at 07:57 PM 0
Share

Does the object that has this script attached (the zombie?) have a Nav$$anonymous$$eshAgent component as well?

avatar image DARTHMAYDAR11 Taxen0 · Feb 02, 2016 at 05:48 AM 0
Share

yes it does

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

43 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ObjectReference Not set to an instance of an object 1 Answer

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

How to access a script from another without using GameObject.Find() 1 Answer

Null Reference Exception only in second method 1 Answer

Scripts working in Editor, but not in build 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges