Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 cindycharvet · Jul 08, 2021 at 02:53 AM · collider2ddisablepolygon collider 2ddisable object

How to disable many colliders in the same game object

Hi! I have a Game Object which has 4 polygon colliders 2D that change depending on which frame of the animation is played by the sprite... now I want to disable them but I can't find the way to do that... any help would be really appreciated @Hellium

Thanks! Here is part of the script that Im using

 [SerializeField]
 public PolygonCollider2D[] colliders;
 private int currentColliderIndex = 0;

 void Die()
 {
     animator.SetBool("IsDead", true);
     foreach(PolygonCollider2D other in GetComponent<PolygonCollider2D>())
     {
         other.enabled = false;
     }
     this.enabled = false;
 }

 public void SetColliderForSprite( int spriteNum )
 {
     colliders[currentColliderIndex].enabled = false;
     currentColliderIndex = spriteNum;
     colliders[currentColliderIndex].enabled = true;
 }


}

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

4 Replies

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

Answer by KarMa_PlaYzz · Jul 08, 2021 at 01:37 PM

Instead of going for 'GetComponent' and 'PolygonCollider2D' your best bet is to switch to 'GetComponents' and 'Collider2D' instead.

GetComponents< Type>(): returns all the components of type on the same gameobject

Collider2D: instead of specifying which collider you want you can use Collider2D instead, this gets all the colliders on the gameobject. (If this isn't what you want, return to using 'PolygonCollider2D'

Hope this helps!

  [SerializeField]
  public PolygonCollider2D[] colliders;
  private int currentColliderIndex = 0;

  void Die()
  {
      animator.SetBool("IsDead", true);
      foreach(Collider2D col in GetComponents<Collider2D>())
      {
          col.enabled = false;
      }
      this.enabled = false;
  }

  public void SetColliderForSprite(int spriteNum)
  {
      colliders[currentColliderIndex].enabled = false;
      currentColliderIndex = spriteNum;
      colliders[currentColliderIndex].enabled = true;
  }
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
1

Answer by TCemAlpaydin · Jul 08, 2021 at 07:33 AM

I don't think your question is very clear, but I'll share what I know. You can just tell me if it is what you're after. If I were in your shoes, I would just enable/disable the colliders inside the animation. Though if you want to use your method, the scripts you've shared should do it. You just need to make sure you drag the right ones in the inspector. If you're looking for the mistake in your "Die()" function, it has a syntax error, you can use either one of these instead.

  void Die()
  {
      animator.SetBool("IsDead", true);
      foreach(PolygonCollider2D other in GetComponents<PolygonCollider2D>())
      {
          other.enabled = false;
      }
      this.enabled = false;
  }

  void Die()
  {
      animator.SetBool("IsDead", true);
      foreach(PolygonCollider2D other in colliders)
      {
          other.enabled = false;
      }
      this.enabled = false;
  }

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 Anonymous620 · Jul 08, 2021 at 04:45 AM

 for(int i = 0; i < 5; i++){
       colliders[i].enabled = false;
 }

This code runs through your array of polygon colliders and disables them. Edit: Sorry did you want to disable a certain collider or all at once

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 cory48 · Jul 08, 2021 at 05:11 PM

Use GetComponentsInChildren on the parent object to get all colliders. And myaarpmedicare for those colliders who 'isTrigger' is true, set enabled false.

  foreach(var c in gameObjectParentName. GetComponentsInChildren<Collider>())
  {
  if(c. isTrigger) c. enabled = false;
  }


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

126 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

Related Questions

Can CompositeCollider2D not be disabled? 1 Answer

How to edit PolygonCollider2D vertices IN CODE? 0 Answers

Why can I not directly set the points of PolygonCollider2D? 0 Answers

Trouble with composite and polygon collider in player 0 Answers

How to create a 3d polygon collider? 3 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