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 /
avatar image
0
Question by jmparavicini · Jan 21, 2016 at 05:13 AM · c#collisionerror

OnTriggerEnter throws an error but code still works

HI Guys, I'm trying to make a turret shoot a minion if its on the enemy team the code works actually just fine but it still throws this error NullReferenceException: Object reference not set to an instance of an object Tower.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/Tower.cs:28) I'm not sure why this happens but i know that it's really annoying i really hope someone can give me an idea or a hint of why this happens.

Here's my script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Tower : MonoBehaviour {
 
     public List<GameObject> enemysList;
     private GameObject enemyAttacking;
     private LineRenderer lineToEnemy;
 
     public GameObject m_BulletPrefab;
     public float m_BulletSpeed;
     public float m_TimeBetweenBullets;
     public float m_TimeBetweenBulletsLeft;
 
     public float m_damage;
 
     void Start()
     {
         lineToEnemy = GetComponent<LineRenderer>();
         m_TimeBetweenBulletsLeft = m_TimeBetweenBullets;
     }
 
     void OnTriggerEnter (Collider col)
     {
         if (col.gameObject.CompareTag("Minion"))
         {
             if (col.gameObject.GetComponent<CurrentTeam>().Team != GetComponent<CurrentTeam>().Team)
             {
                 enemysList.Add(col.gameObject);
             }
         }
             
     }
 
     void OnTriggerExit(Collider col)
     {
         if (enemysList.Contains(col.gameObject))
         {
             enemysList.Remove(col.gameObject);
         }
         if (enemyAttacking == col.gameObject)
             enemyAttacking = null;
     }
 
     void Update () 
     {
 
         if (m_TimeBetweenBulletsLeft > 0)
             m_TimeBetweenBulletsLeft -= Time.deltaTime;
 
         m_TimeBetweenBulletsLeft = Mathf.Clamp(m_TimeBetweenBulletsLeft, 0f, m_TimeBetweenBullets);
         
         if (enemyAttacking != null)
         {
             lineToEnemy.SetVertexCount(2);
             lineToEnemy.SetPosition(0, transform.position);
             lineToEnemy.SetPosition(1, enemyAttacking.transform.position);
 
             if (m_TimeBetweenBulletsLeft == 0)
                 Shoot(enemyAttacking);
         }
         else if(enemyAttacking == null)
         {
             lineToEnemy.SetVertexCount(0);
         }
         if (enemyAttacking == null && enemysList.Count != 0)
             enemyAttacking = EnemyToAttack();
     }
 
     void Shoot(GameObject enemyToAttack)
     {
         m_TimeBetweenBulletsLeft = m_TimeBetweenBullets;
         GameObject clone = Instantiate(m_BulletPrefab, transform.position, Quaternion.identity) as GameObject;
         clone.GetComponent<towerBullet>().GetTarget(enemyToAttack, m_BulletSpeed, m_damage);
         clone.GetComponent<towerBullet>().myTeam = gameObject.GetComponent<CurrentTeam>().Team;
     }
 
     GameObject EnemyToAttack()
     {
         return enemysList[Random.Range(0, enemysList.Count)];
     }
 }


as you can see from the error the "wrong" part is at line 28. Thanks in advance Cheers skullbeats1

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jan 21, 2016 at 05:41 AM

Well, a line like yours is in general a bit "risky". You use GetComponent and assume that the component you search for exists. According to the error it most likely doesn't exist, either on the "Tower" or the object your Tower touches.

To optimise and debug the problem i would suggest:

private CurrentTeam myTeam;

 void Start()
 {
     // ... 
     myTeam = GetComponent<CurrentTeam>();
     if (myTeam == null)
         Debug.LogWarning("The Tower script should have a 'CurrentTeam' component but none was found", gameObject);
 }
 
 void OnTriggerEnter (Collider col)
 {
     if (col.gameObject.CompareTag("Minion"))
     {
         CurrentTeam colTeam = col.gameObject.GetComponent<CurrentTeam>();
         if (colTeam == null)
             Debug.LogWarning("A Minion entered the trigger of a Tower but that Minion doesn't have a CurrentTeam attached next to the collider", col.gameObject);
         if (colTeam.Team != myTeam.Team)
         {
             enemysList.Add(col.gameObject);
         }
     }
 }

This won't stop the error but should help to pinpoint the problem. If you get any of those warnings, click on it and it should highlight / ping the object in question. If both objects have a CurrentTeam script attached, you might collider with an unexpected collider. You can use a method like the following to print the object path in the hierarchy:

 public static class GameObjectPathExtension
 {
     public static string GetPath(this GameObject aObj)
     {
         if (aObj == null)
             return " - null - ";
         string path = aObj.name;
         Transform c = aObj.transform.parent;
         while (t != null)
         {
             path = t.name + "/" + path;
             t = t.parent;
         }
         return path;
     }
 }

This extension allows you to easily print the hierarch path of an object. For example:

 Debug.Log("Path is: " + col.gameObject.GetPath());

This might help to figure out why the component isn't found on that object.

Comment
Add comment · Show 1 · 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 jmparavicini · Jan 21, 2016 at 05:52 AM 0
Share

Thanks for your fast reply I fixed the problem i had an reference in the same script but i changed the style of the game and made it so that every $$anonymous$$m based object should have the CurrentTeam script but i only added this script to one tower. Hehe Ups

Well thanks for your fast reply it helped me to find the error

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

67 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 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

Compiler error while trying to access a Collider's script. 1 Answer

Multiple Cars not working 1 Answer

Raycast doesn't collide as it should 1 Answer

Error CS1061 I can't find a solution! [C#] 1 Answer

Can you help me about a animation ? 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