Help with [SerializeField] in a script!

Hello everyone, I have a script that works, but my issue is that I have to put it multiple times and drag a box collider 2d to get it to work.

What I’m trying to achieve is to somehow be able to drag the box colliders to the same script so I don’t have to have the same script multiple times with different colliders.


Here is my script

public class IgnoreCollision : MonoBehaviour {


    [SerializeField]
    private Collider2D other;
	// Use this for initialization
	private void Awake()
    {
        
        Physics2D.IgnoreCollision(GetComponent<Collider2D>(), other, true);
	}
	
}

Thank you for the help!

Simply convert the type of the field to a collection, such as an array or a List:

public class IgnoreCollision : MonoBehaviour {
    // Notice the array operator ([]) and the pluralization
    [SerializeField] Collider2D[] others;  

    void Awake() {
        foreach (Collider2D other in others)
            Physics2D.IgnoreCollision(GetComponent<Collider2D>(), other, true);
    }
}

Since you’re trying to do it this way, you can edit the Physics system to prevent collisions between layers. Edit > Project Settings > Physics and Physics2D. The triangle-shaped set of checkboxes allow collision for layers where the boxes intersect. Unchecking these disables collision between these layers. Something to consider, at least.