Search Unity

Identify both objects that is colliding from one scrip

Discussion in 'Scripting' started by pKallv, Nov 26, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I have two similar game objects on my scene that act as player protection areas, meaning that objects that is within these areas is protected.

    The two areas have rigidbody2d and collider2d attached and the player objects have collider2d attached.

    I have a script attached to the areas with "Void OnTriggerStay2D(Collider2D col) {".

    This is triggered when i move a player object into the player protection areas and i am also able to identify these objects using the "col.tag" parameter.

    However, is it possible to also identify the player protection area GameObjects in the same script?
     
  2. Peter Ples

    Peter Ples

    Joined:
    Mar 12, 2013
    Posts:
    237
    Do you mean getting the gameObject of the area you are colliding with? (if the script is on the player)

    If so, the variable gameObject is an inherited member of Collider2D and you can call col.gameObject. Most if not all components can do that.

    But your wording is that the script is on the areas themselves. If that's the case you already know the objects, no? If you want to get all the area objects you could tag them and use http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html I use tags in my personal projects but in a big collaborative project like at work we don't use tags. We find the objects that have the script like this: http://answers.unity3d.com/questions/46283/way-to-get-all-object-with-a-certain-componentscri.html

    I'm a little confused by the question but hopefully this info helps
     
  3. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    The object you're looking for will be the object that the OnTriggerStay is executing against - so you can reference it simply as "gameObject" in code. The other object (in this case the Player) is the collider that you've called "col" (col.gameObject for it's GameObject).

    Code (csharp):
    1.  
    2. private void OnTriggerStay(Collider col)
    3. {
    4.     Debug.Log(string.Format("{0} - I'm the player area that has been collided with.", gameObject.name));
    5.     Debug.Log(string.Format("{0} - I'm the object that has entered the area.", col.gameObject.name));
    6. }
    7.  
    EDIT: Just for completeness, you might not want to use OnTriggerStay in your case. I would think that OnTriggerEnter you'd set a player flag like IsProtected = true, and then OnTriggerExit, IsProtected = false.
     
    Last edited: Nov 27, 2014
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Thanks, should have understand this from start :)
     
  5. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    No - as far as questions go, that's a decent one. When I started using Unity, I asked some genuinely dumb questions :)
     
  6. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191