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

how to distinguish two "Box Collider 2D" in the script?

Discussion in 'Editor & General Support' started by coldpl_ay, Apr 19, 2014.

  1. coldpl_ay

    coldpl_ay

    Joined:
    Jan 20, 2014
    Posts:
    36
    Hi,everybody!


    In the Inspector, I have two "Box Collider 2D", each has a script to process different function. But how to distinguish two "Box Collider 2D" in the script?
    Below is the sample:

    void OnTriggerEnter2D(Collider2D other) -------------how to distinguish "other"?
    {
    ........
    }

    $QQ??20140419231010.jpg
     
    Last edited: Apr 19, 2014
  2. coldpl_ay

    coldpl_ay

    Joined:
    Jan 20, 2014
    Posts:
    36


    Now I've learned using like this:
    "Collider2D[] cols = GetComponents<Collider2D>();"
    But I want to use the first one, "cols[0]" is wrong. Could anybody tell me how to use the first one?
     
  3. sebrobitaille

    sebrobitaille

    Joined:
    Mar 9, 2013
    Posts:
    65
    If the positions or the size stays the same you could check that
     
  4. coldpl_ay

    coldpl_ay

    Joined:
    Jan 20, 2014
    Posts:
    36
    your meaning is using "foreach(Collider2D one in cols))", then judge every one on its own property?
    like this?

    foreach (var t in GetComponentsInChildren<Transform>())
    {
    if (!originalStructure.Contains(t))
    {
    t.parent = null;
    }
    }


    But is there a another way to use the first one, like "cols[0]" ? thanks:)
     
    Last edited: Apr 21, 2014