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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Get Array BoxCollider2D

Discussion in 'Scripting' started by behdadsoft, Jan 30, 2018.

  1. behdadsoft

    behdadsoft

    Joined:
    Aug 25, 2012
    Posts:
    150
    Hi.

    In my scene i have many box objects that they contain many box child and i want get all box child colliders in Start() function like below, but it only can get box child colliders in one box objects. how can get all of them?

    Code (CSharp):
    1.  
    2. private BoxCollider2D[] BoxCollider;
    3.  
    4.     void Start ()
    5.     {
    6.       BoxCollider = GameObject.FindGameObjectWithTag("BoxObject").GetComponentsInChildren<BoxCollider2D>();
    7.     }
    8.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    If you just want to grab all the box colliders at start, the simplest call is just FindObjectsOfType<BoxCollider2D>().

    The code you have above finds a single game object by name (the first one called "BoxObject"), then gets the BoxCollider2Ds on that one game object, which is why you only get the one.

    As an aside, don't run that code every frame. It will kill your performance. At scene startup is fine obviously, as it won't be noticed.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    He beat me to it, but another option (if possible) is to add [SerializeField] on your array and simply drag every single one in from the inspector ;) lol

    If getting every type doesn't work, use the plural for tags. Still better to use the inspector, imo, but nevertheless, there is the method: https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

    Using that you can collect all of the box colliders. Probably best with a list, that you can then convert to an array for your variable.
     
  4. behdadsoft

    behdadsoft

    Joined:
    Aug 25, 2012
    Posts:
    150
    Thanks for reply.

    In my scene there are many BoxCollider2D and i need get only BoxCollider2D for BoxObject. because i want disable and enable isTrigger for them.
     
    Last edited: Jan 30, 2018
  5. behdadsoft

    behdadsoft

    Joined:
    Aug 25, 2012
    Posts:
    150
    Thanks @methos5k

    seem i don't have better choice and should be add them in inspector.;)
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The inspector is by far the best, if it's available (I mean if they're in the scene).

    You're welcome.
     
  7. behdadsoft

    behdadsoft

    Joined:
    Aug 25, 2012
    Posts:
    150
    i forget one thing that for each BoxObject, should i add new BoxCollider2D[] variable?
     
    Last edited: Jan 31, 2018
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you want 1 array of child box colliders for each BoxObject?

    Or 1 array with every box collider of box object and children? :)
     
  9. behdadsoft

    behdadsoft

    Joined:
    Aug 25, 2012
    Posts:
    150
    yes. but it seem impossible. right?

    EDIT: i want when i collide to each BoxObject, all child box collider save to BoxCollider2D[].
    i do this with OnCollisionEnter2D(), but this way make exist some problem.
     
    Last edited: Jan 31, 2018
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Nope. If you want one per BoxObject (1 array for itself + all children), I would say you should try this:
    Code (csharp):
    1. BoxCollider2D [] boxes;
    2. void Start() {
    3.    boxes = GetComponentsInChildren<BoxCollider2D>();
    4.  }
    5.  
    Edited this portion: That includes all children and children of children, etc..
    (before the edit, I thought maybe it was only 1 deep. should have checked first ) :)
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    According to your edit, I'm not sure there's any difference. Unless the children are modified at runtime, then it's the same if you store them early or on collision. By 'same', I mean you'll get same list. It's better to store them once (either from the inspector), or getting all child components in Start(), though. That's compared to fetching them anew on collision.
     
  12. behdadsoft

    behdadsoft

    Joined:
    Aug 25, 2012
    Posts:
    150
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :)