- Home /
Trigger not getting GameObject
Hi, I'm having a trouble with a trigger that won't get a GameObject and always give me a NullReferenceException.
I have one character with a big Sphere Collider as an empty child. "Is trigger" is checked and the trigger function works but when I try to get the other GameObject, it will always return null.
This is the code I use:
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
currentEnemy = other.GetComponent<EnemyController>().listIndex(0, false);
enemiesInRange.Add(Spawner.enemyPool[currentEnemy]);
}
}
What I'm trying to do with "currentEnemy" is get the index of the object in the pool (the pool was previously made in the Spawner script) so I can add that object to a private list of enemies that are in range. I'm sending (0, false) just because I use the same function to also write objects in the pool from the spawner, so it's just telling it that I'm not adding anything, I only want the return.
I also tried:
if (other.tag == "Enemy")
{
EnemyController enemy = other.GetComponent<EnemyController>();
currentEnemy = enemy.listIndex(0, false);
enemiesInRange.Add(Spawner.enemyPool[currentEnemy]);
}
But also no luck. I'm using this code in another script and it works fine:
if (other.tag == "Enemy")
{
EnemyController enemyController = other.GetComponent<EnemyController>();
if (enemyController != null)
enemyController.takeDamage(damage);
}
What am I doing wrong?
Thank you!
EDIT: The line that gives me the NullReference is
currentEnemy = other.GetComponent().listIndex(0, false);
or, in the second script:
EnemyController enemy = other.GetComponent();
$$anonymous$$aybe the problem is that the other doesn't have an EnemyController on it, so GetComponent() returns null and you try to access listIndex on a null. Or it is also possible that listIndex is not initialized on that EnemyController.
Can you check what happens if you check if your enemy is null or not (just like in your last example), and also if enemy.listIndex is null?
The other has an EnemyController on it because I also get it on the script I use on my bullets and when they hit, they do return it and I am able to do: enemyController.takeDamage(damage) to damage the enemy.
The problem only happens when I try to get it from a particular script.
Even if listIndex is not initialized (or doesn't even exist), I should still be able to get the EnemyController.
I can try to get listIndex with the script the bullets use and see if that works just to make sure the problem is not on the enemy's script end.
Yes, please try that, and also, which line throws the NullRefereceException (the error message usually tells you the line)? It would help a lot to know where exactly the error is co$$anonymous$$g from. $$anonymous$$aybe it is in the class of listIndex
(BTW, what is the type of listIndex
?)...
Answer by juliobln224 · Feb 23, 2018 at 04:07 PM
It finally works!
Thanks to @Harinezumi I found the error.
I had one parent with a rigidbody and two colliders on child objects (with no rigidbody) and when I was trying to get the script from them, they didn't give me anything back because the script was on the parent.
There are a bunch of ways to solve this, like having scripts on colliders and basically relaying them to the parent but what I use is GetComponentInParent as in my case, it works great because I only have one component of that type (EnemyController) in any of the parents. I don't know if this is the best or most optimized way to use it for something like bullets hitting enemies (there are a lot of bullets) but it works.
This is the result:
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
currentEnemy = other.gameObject.GetComponentInParent<EnemyController>().listIndex(0, false);
enemiesInRange.Add(Spawner.enemyPool[currentEnemy]);
}
}
Great that you have solved the issue! :) GetComponentInParent() is certainly an acceptable way of solving this.
Answer by Jasmin1347 · Feb 23, 2018 at 04:13 AM
change if statement
if(other.collider.tag == "Enemy")
{
// your code
}
Thank you but it doesn't help.
The problem is not getting into the if statement, I had this before:
if (other.tag == "Enemy")
{
EnemyController enemy = other.GetComponent<EnemyController>();
**Debug.Log(enemy == null);**
currentEnemy = enemy.listIndex(0, false);
enemiesInRange.Add(Spawner.enemyPool[currentEnemy]);
}
And I was always getting "True" in the debug console so the If statement is working fine.
This definitely means that the object that you are colliding with (or that triggers) does not have EnemyController script on it.
Could it be that you are colliding with a child game object of your enemy, but the EnemyController is on a parent?
Now that you say it...I removed the collider from the parent and put colliders on the children (to get a more precise hit) and I think the issue started after changing that. I can't remember if it was just some enemy or the enemy prefab so I'll have to check it when I get home but it may actually be that silly thing.
I will check it and let you know!
Collider.collider is deprecated since Unity 5, and would return the same Collider anyway. This is not the cause for the issue.
Your answer
Follow this Question
Related Questions
Select collider for GetComponent? 1 Answer
Return gameobject from a TriggerEvent. 1 Answer
I need help with triggers 1 Answer
Triggers Interacting with Triggers 0 Answers
How to run a function in a GO that have DontDestroyOnLoad 1 Answer