Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DonHaul · Jan 19, 2015 at 05:52 PM · 2dcolliders

How to detect multiple Colliders in the same object?

Hello, I am making a 2D game, where the players has 2 Colliders, (a Circle and a Box Collider 2D) And at some point I use, to transform the player colliders into triggers:

 void OnTriggerStay2D(Collider2D other)
     {
         if (other.gameObject.tag == "Player") {
               other.gameObject.collider2D.isTrigger = true;
                 }

But this way it only acesses the first collider in the players inspector and not both...

Can anyone tell me how to detect both colliders?

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
1

Answer by spiceboy9994 · Jan 19, 2015 at 06:40 PM

Additional to Simon Larsen's answers you may want to access specific properties from each collider type so, it could be:

 void OnTriggerStay2D(Collider2D other)
  {
      if (other.gameObject.tag == "Player") {
          foreach (Collider2D collider in other.gameObject.GetComponents<Collider2D>())
          {
 
              collider.isTrigger = true;
              if (collider is BoxCollider2D) {
                  BoxCollider2D myBoxCollider = (BoxCollider2D)collider;
                   //Do some stuff with the box collider
              }
              else if (collider is CircleCollider2D) {
                 CircleCollider2D myCircleCollider = (CircleCollider2D)collider;
                 //Do some stuff with the cyrcle collider   
              }
          }
      }
  }


Hope this helps

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 DonHaul · Jan 21, 2015 at 09:10 PM 0
Share

thanks :D

avatar image
0

Answer by Simon-Larsen · Jan 19, 2015 at 06:11 PM

You could fetch all the components of a certain type and iterate through the components by doing the following.

 void OnTriggerStay2D(Collider2D other)
 {
     if (other.gameObject.tag == "Player") {
         foreach (Collider2D collider in other.gameObject.GetComponents<Collider2D>())
         {
             collider.isTrigger = true;
         }
     }
 }
Comment
Add comment · Show 2 · 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 Simon-Larsen · Jan 19, 2015 at 06:14 PM 0
Share

In case some of your colliders are placed as children on the collider you could also search through the children http://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html

avatar image DonHaul · Jan 21, 2015 at 09:10 PM 0
Share

thank you very much ! I always forgot about those usefull foreach loops :/

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Coins and Points 1 Answer

Colliders 2d apparently touch each other even if they should not. 2 Answers

Do I really have to do this? 2 Answers

Strange Collisions in 2D 1 Answer

Player with RigidBody2D "bouncing" on EdgeCollider 0 Answers


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