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 Trappedlol · Apr 24, 2018 at 06:45 PM · collider2dontriggerenter2d

Area of Effect (AoE) Stun Issue

I've tried to create a trigger which causes an area of effect stun towards my enemies. This is what I got so far.

 private void OnTriggerEnter2D(Collider2D other)
 {
     if(other.gameObject.tag == "Enemy")
     {
         Collider2D[] enemies = Physics2D.OverlapCircleAll(transform.position, 10, 1 << LayerMask.NameToLayer("Foreground"));

         foreach(Collider2D en in enemies)
         {
             Rigidbody2D rb = GetComponent<Rigidbody2D>();
             EnemyBehaviour stun = rb.GetComponent<EnemyBehaviour>();
             stun.EnemyStunned();
         }
         Destroy(this.gameObject);
     }
 }


I get this error: NullReferenceException: Object reference not set to an instance of an object HunterTrapBehaviour.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/RPG Project - R&T/_Scripts/HunterTrapBehaviour.cs:19)

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

2 Replies

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

Answer by Kciwsolb · Apr 24, 2018 at 07:27 PM

On line 10 of the code you posted, you are setting rb to the value of GetComponent. GetComponent will try to return a Component on the GameObject you run it on. You probably need to try Rigidbody2D rb = en.GetComponent<Rigidbody2D>();

Also, you are assuming that rb is not null. You probably should check for that possibility before trying to get your EnemyBehaviour script. And before you try to call EnemyStunned(), you might want to be sure that stun is not null as well.

Even though you are checking for a tag for the Collider passed into OnTriggerEnter2D, you still need to check if those Components exist because OverlapCircleAll is returning an array of all Colliders in a radius. This array is unrelated to the one Collider you already checked. So, the Colliders being returned might not all be enemies.

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 Trappedlol · Apr 25, 2018 at 03:42 PM

Thank you! I can't believe I missed that. For anyone dealing with a similar issue in the future this is the working script.

   private void OnTriggerEnter2D(Collider2D other)
 {
     if(other.gameObject.tag == "Enemy")
     {
         Collider2D[] enemies = Physics2D.OverlapCircleAll(transform.position, 10, 1 << LayerMask.NameToLayer("Foreground"));

         foreach(Collider2D en in enemies)
         {
             Rigidbody2D rb = en.GetComponent<Rigidbody2D>();
             if(rb != null && rb.tag == "Enemy")
             {
                 EnemyBehaviour stun = rb.GetComponent<EnemyBehaviour>();
                 stun.EnemyStunned();
             }
         }
         Destroy(this.gameObject);
     }
 }
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

83 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

Related Questions

Object is not colliding with wall colliders 0 Answers

Can you check again something that is already in the IsTrigger collider ? 2 Answers

OnTriggerEnter2D works without being actually triggered. 0 Answers

OnTriggerEnter2D - am I getting it wrong? 2 Answers

OnTriggerEnter2D & OnCollisionEnter2D Not Responding 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