- Home /
Select collider for GetComponent?
I have 2 colliders on my object, one is set to trigger the other one is not. What should I add to my code to select the radius of the trigger collider?
jumpEnablerRadius = player.transform.GetComponent<CircleCollider2D>().radius;
This code returns the radius of the collider that I use for physics, and therefore is not a trigger. I tried using an if statement with .isTrigger , but I can't get it to work.
I also tried using GetComponents, but I can't seem to extract the trigger out of the array it returns, I have very bad understanding of loops.
Edit:
Simon really nailed it. Here's the adapted code for people who stumble on this question.
CircleCollider2D[] colliders = player.transform.GetComponents<CircleCollider2D>();
foreach (CircleCollider2D collider in colliders)
{
if (collider.isTrigger) {
jumpEnablerRadius = collider.radius;
Debug.Log ("Jumper radius set" + jumpEnablerRadius.ToString());
}
}
Answer by Simon-Larsen · Apr 01, 2014 at 11:45 AM
I posted a little code for you to take a look at. It will get all the CircleCollider2D on on the player GameObject. Hope this helps you. Let me know in the comment if you continue to have issues with or if there are compiler errors (haven't tested the code).
public class Example : MonoBehaviour {
void Example() {
CircleCollider2D[] colliders = player.transform.GetComponents<CircleCollider2D>();
foreach (CircleCollider2D collider in colliders) {
if (collider.isTrigger) {
// This is collider is a trigger
}
}
}
}
Yeah that looks like something I tried, however I couldn't set up my foreach loop properly. Like I said, I suck big time with for/foreach/while loops, I just can't understand how to get ONE element out of an array. Anyway thanks for the answer, its perfect. Too bad it turned out that this whole thing was pointless, since apparently Physics2D.OverlapCircle returns true even when the collider shares edges with other colliders.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141654im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trigger not getting GameObject 2 Answers
Trigger not working 2D 1 Answer
Weird behaviour with OnTriggerEnter 0 Answers
Ideas for detecting collision speed with trigger? 2 Answers
Trigger collider information 3 Answers