Unity multiple collider on same object
Asked Answered
A

5

11

I am using several box colliders on the same GameObject, the question is, is it possible to refer to each of those colliders separately using
gameObject.getComponent().enabled = false;
Keep in mind that I have few colliders and I want some of them to stay enabled while the others will be disabled.

Aquamarine answered 8/5, 2015 at 21:7 Comment(4)
You could find them all with GetComponents (or GetComponentsInChildren), but how would you tell them apart? In circumstances like this, I frequently end up attaching an extra script that keeps track of the other components via lists/arrays that I set up in the inspector.Cusped
Yea that is exactly the issue I was hoping that in unity 5 they would already have numbering or naming to colliders ... really frustrating :/Aquamarine
you can just save each collider in an array and can then access them one by one. Do you have to know which one is which or do you just one them in arrays so that you can access them using indexes?Busboy
Specifically the problem was solved because I needed only 3 colliders which made it possible to have a poly + box + circle and then it was easy to refer to each one by itself but I'm assuming that for more than 3 colliders a collider array is a mustAquamarine
M
13

What I do is create empty child GameObjects [One GameObject for every collider] and assign colliders to them, and then I would assign tags to those children [in your case they can be 'AlwaysActive' and 'SwitchingActive' or whatever better name you can come up with].

Then in the parent I will use GetComponentsinChildren<Collider> to find all the colliders, and check if the tag of the GameObject (of a respective collider) matches my requirement or not. If it does I will do my required task otherwise skip it.

NOTE: Colliders on child GameObjects do not call OnTriggerEnter or OnCollisionEnter in parent script. I use delegate strategy to get collisions from child game objects into my parent game object.

Moraceous answered 17/6, 2015 at 8:1 Comment(2)
"NOTE: Colliders on child GameObjects do not call OnTriggerEnter or OnCollisionEnter in parent script." --- This was the information I was looking for, thanks!Sofa
Could you elaborate the delegate strategy to get collisions from the child? This is what I'm trying to do but don't know how.Affenpinscher
I
1

You can generate all colliders by script

var collider1 = gameObject.AddComponent<BoxCollider>();  
var collider2 = ...  
...  

then you can set all properties for each collider
https://docs.unity3d.com/ScriptReference/Collider.html
So you can use every collider by it variable as you want

Intarsia answered 5/8, 2022 at 11:57 Comment(0)
A
0

Old but maybe it helps: you can get multiple Components in a gameObject:

  Collider[] colls = GetComponents<Collider>();
             foreach(var col in colls){
                 col.isTrigger = false;
             }
Anaphora answered 13/4, 2023 at 15:9 Comment(0)
C
0

This believe it or not worked for me, if you're doing 2d. Drag this script onto whatever object has your triggers on it, then drag in your player to the public variable reference slot and make sure you have a BoxCollision2d on it. I'm sure you could use it for 3d you have to change the Collider2D parts though to just "collider" or something (I'm guessing).

The numbers in the array start at 0 and will correspond to whatever BoxCollider is 0th, 1st, and 2nd in the inspector on the right, you can change the number 2 to whatever collider you need.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ur_scriptName: MonoBehaviour
{
   public Collider2D[] colliders;
   public GameObject player;

   void Start()
        {
        colliders = gameObject.GetComponents<Collider2D>();
        }

   void Update()
        {

        }

   void OnTriggerEnter2D(Collider2D other)
        {
            if (colliders[2].IsTouching(player.GetComponent<Collider2D>()))
            {
            Debug.Log("player touching collider 2");
            }
        }
}
Congregational answered 25/1 at 4:39 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Snipes
This actually works btw, Thought I would post here cuz it was in the top 10 google results when I had this problem and no one had the right answer.Congregational
K
0

Kinda surprised nobody's mentioned the easy option!

Declare the collider(s) you want to reference as public variables on the script you wish to access them from, thus:

public BoxCollider2D collider;

Now, in the object inspector, locate the script. It should have a new entry called 'collider' (or whatever you called it). Just drag the collider into that box to link the script variable to the collider.

Kaule answered 26/3 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.