- Home /
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
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.
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
Follow this Question
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