Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by LT23Live · Aug 25, 2014 at 05:11 AM · gameobjectnullreferenceexceptionontriggerenter

OnTriggerEnter -- NullReferenceException -- C#

here is the code I Recieve NullReferenceException: Object reference not set to an instance of an object NPC_Battle_AI.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Assets/Scripts/NPC_Battle_AI.cs:82)

I added a self check but it still popped up null. I have attached the script. Can anyone Help?

 using UnityEngine;
 using System.Collections;
 
 public class NPC_Battle_AI : MonoBehaviour {
 
     public GameObject NPC;
     public float rotationSpeed;
     public GameObject target;
     public bool move;
     public bool attack;
 
     public GameObject[] projectileSpawn;
     public GameObject projectile;
     private GameObject missile1 ;
     private GameObject missile2 ;
     public float reloadTine;
     public GameObject closest;
     public bool openBattle;
 
     void Start ()
     {
         if (target == null)
         {
             openBattle = true;
             move = false;
             NPC = gameObject;
             rotationSpeed = 5;
         FindClosestEnemy(target);
         }
     }
 
     void Update ()
     {
 
         if (move == true)
         {
         NPC.transform.rotation = Quaternion.Slerp (NPC.transform.rotation, Quaternion.LookRotation (target.transform.position - NPC.transform.position), rotationSpeed * Time.deltaTime);
         transform.position = Vector3.MoveTowards (transform.position,target.transform.position, GetComponent <Stats_Script>().speed *Time.deltaTime );
         }
         else if (attack == true)
         {
             Attack ();
 
             if (target == null)
             {
                 attack = false;
                 GetComponent <NPC_Battle_AI>().enabled = false;
                 gameObject.GetComponent<Basic_AI>().enabled = true;
 
             }
         }
     }    
 
     void FindClosestEnemy(GameObject target) {
         if (openBattle == true )
             {
             GameObject[] gos;
             gos = GameObject.Find ("Main Camera").GetComponent <Tower_Database>().registry;
 
             GameObject closest;
             float distance = Mathf.Infinity;
             Vector3 position = transform.position;
             foreach (GameObject go in gos) {
                 Vector3 diff = go.transform.position - position;
                 float curDistance = diff.sqrMagnitude;
                 if (curDistance < distance) {
                     closest = go;
                     target = closest;
                     distance = curDistance;
                 }
             }
     //        return closest;
         }
     }
 
     void OnTriggerEnter (Collider col)
     {
         if (col != null)
         {
         if (openBattle == true)
         {
                 if (col.gameObject.GetComponent <Stats_Script>().userActive == true)
                 {
                     target = col.gameObject;
                     move = false;
                     attack = true;
                 }
             }
         }
     }
 
     void Attack ()
     {
         attack = false;
         if (projectileSpawn.Length == 0)
         {
             gameObject.GetComponent <NPC_Battle_AI>().enabled = false;
             gameObject.GetComponent <Basic_AI>().enabled = true;
         }
 
         else {
             missile1 = Instantiate (projectile, projectileSpawn[0].transform.position, projectileSpawn[0].transform.rotation) as GameObject;
             if (projectileSpawn.Length > 1 )
             {
                 missile2 = Instantiate (projectile, projectileSpawn[1].transform.position, projectileSpawn[1].transform.rotation) as GameObject;
             }
 
             StartCoroutine (Wait());
         }
     }
     
     IEnumerator Wait ()
     {
         print (gameObject.name + " Reload.");
         yield return new WaitForSeconds (reloadTine);
         attack = true;
     }
 
 }
 
Comment
Add comment · Show 1
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 Khada · Sep 06, 2014 at 01:27 PM 0
Share

Any luck dude?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Khada · Aug 25, 2014 at 05:14 AM

The '.userActive' assumes that 'GetComponent' found a component of the type 'Stats_Script' on the 'gameObject' from 'col'. It would seem to be a false assumption.

Try storing the return value of 'GetComponent' and checking if it's null before using it.

 Stats_Script s = col.gameObject.GetComponent <Stats_Script>();
 if(s != null && s.userActive == true)
     //...
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
avatar image
0

Answer by Tomer-Barkan · Aug 25, 2014 at 05:17 AM

Like robertu said, most likely the colliding object does not contain the script Stats_Script on it.

You should add a test for this in your code, and ignore colliders that do not have this script:

 Stats_Script statsScript = col.gameObject.GetComponent <Stats_Script>();
 if (statsScript != null && statsScript.userActive == true)
 {
     target = col.gameObject;
     move = false;
     attack = true;
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

NullReferenceException 0 Answers

How to destroy and trigger on enter. 1 Answer

I'm trying to create a count of how many hearts I collect and a NullReferenceException appears each time? 0 Answers

The name 'gameObject' does not denote a valid type? 1 Answer


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