I have many colliders and I want to avoid having one script on each object and use OnTriggerEnter2D() for each of them.
Instead, I want to manage all collisions from one script.
So basically, what I want to do is:
public BoxCollider2D myCollider1 ;
public BoxCollider2D myCollider2 ;
if myCollider1 is triggered {
what is the tag of the colliding object?
}
You can’t really reference a collider on another object to check if it’s colliding as far as I know. You must simply put a script on each gameObject and then you can send that information to a ‘central’ script if it gets triggered.
Another way you could do it - if your objects are all the same size - is create a script that periodically uses a foreach loop to check the bounds and coordinates of each object in an array. From there, you check which ones appear to be overlapping, and those are the ones colliding.
Something like:
This isn’t tested code, just a concept.
objectsToCheck = GameObject.FindGameObjcetsWithTag("MyTag");
foreach (GameObject object in objectsToCheck){
for (int i = 0;i < objectsToCheck.Length;i++){
if(Vector3.Distance(object.transform.position,objectsToCheck*.transform.position) < gameObject.transform.renderer.bounds.extends.x)*
Debug.Log("This object’s name is " + object.name + "and it is colliding with " objectsToCheck*.gameObject.name);* } }