Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Use a list to define what objects i should ignore the collision

Discussion in 'Scripting' started by unity_BD1000D08C18D3028A82, Dec 5, 2022.

  1. unity_BD1000D08C18D3028A82

    unity_BD1000D08C18D3028A82

    Joined:
    Nov 16, 2022
    Posts:
    15
    I have a GameObject list which holds the objects that i can ignore the collision with, but I'm struggling to get the object's Collider2D element.

    Here it is my foreach loop that runs in a circle overlap list and (at least try) to get the object's collider2D:

    Code (CSharp):
    1.  
    2. List<GameObject> collisors = new List<GameObject>();
    3.  
    4.  
    5. foreach(var colObj in circleOverLap){
    6.     if(collisors.Contains(colObj)){
    7.         Collider2D col = colObj.GetComponent<Collider2D>();  
    8.         Physics2D.IgnoreCollision(col, GetComponent<Collider2D>());
    9.     }
    10. }
    The error return is this
    error CS1503: Argument 1: cannot convert from 'UnityEngine.Collider2D' to 'UnityEngine.GameObject'
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Why are you using
    List<GameObject>
    instead of
    List<Collider2D>
    ?
     
  3. unity_BD1000D08C18D3028A82

    unity_BD1000D08C18D3028A82

    Joined:
    Nov 16, 2022
    Posts:
    15

    Because if I use a list of colliders, I can't put the prefabs in them
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    1. You can absolutely put prefabs in a List<Collider2D>
    2. Why do you want/need to put prefabs in the list? That doesn't make sense. Putting prefabs in this list won't ever work the way you want it to.