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 juliobln224 · Feb 23, 2018 at 03:45 AM · c#gameobjectcollidertriggergetcomponent

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();

Comment
Add comment · Show 4
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 Harinezumi · Feb 23, 2018 at 08:51 AM 0
Share

$$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?

avatar image juliobln224 Harinezumi · Feb 23, 2018 at 09:40 AM 0
Share

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.

avatar image Harinezumi juliobln224 · Feb 23, 2018 at 10:24 AM 0
Share

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?)...

Show more comments

2 Replies

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

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]);
         }
 }


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 Harinezumi · Feb 23, 2018 at 10:23 PM 1
Share

Great that you have solved the issue! :) GetComponentInParent() is certainly an acceptable way of solving this.

avatar image
-2

Answer by Jasmin1347 · Feb 23, 2018 at 04:13 AM

change if statement

 if(other.collider.tag == "Enemy")
 {
  // your code
 }
Comment
Add comment · Show 5 · 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 juliobln224 · Feb 23, 2018 at 06:35 AM 1
Share

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.

avatar image Harinezumi juliobln224 · Feb 23, 2018 at 12:38 PM 1
Share

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?

avatar image juliobln224 Harinezumi · Feb 23, 2018 at 01:43 PM 1
Share

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!

avatar image Harinezumi · Feb 23, 2018 at 08:59 AM 0
Share

Collider.collider is deprecated since Unity 5, and would return the same Collider anyway. This is not the cause for the issue.

avatar image Jasmin1347 · Feb 23, 2018 at 10:59 AM -1
Share

try using Box Collider

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

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

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


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