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 get the position of a gameobject through the collider?!

Discussion in 'Scripting' started by Gistiv, May 9, 2014.

  1. Gistiv

    Gistiv

    Joined:
    Apr 22, 2014
    Posts:
    13
    Hi everyone,

    I'm looking for a solution for my little problem:

    I want to get the gameobjects position in the OnTriggerEnter() function.
    I googled a lot and found many posts but nothing worked...

    some of my solutions which didn't throw a compiler-error but didn't work:

    Code (csharp):
    1.  
    2. public Vector3 Enemyposition;
    3.  
    4. void OnTriggerEnter2D(Collider2D other)
    5.     {  
    6.         Enemyposition = other.transform.gameObject.transform.position;
    7.     }
    8.  
    Code (csharp):
    1.  
    2. public Vector3 Enemyposition;
    3.  
    4. void OnTriggerEnter2D(Collider2D other)
    5.     {  
    6.  
    7.             Enemyposition = new Vector3(other.transform.gameObject.transform.position.x,
    8.                                         other.transform.gameObject.transform.position.y,
    9.                                         other.transform.gameObject.transform.position.z
    10.                                         );
    11.         }
    12.  
    I also tryed it with other.gameObject.transform.position

    I hope you can help me :)

    Greetings Gistiv
     
  2. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    Have you checked if the OnTriggerENter2D actually gets executed? Try placing this in the function:

    Code (csharp):
    1.  
    2. Debug.Log(other);
    3.  
    If nothing appears in your log then something is wrong... Could be one of these things:

    - Object doensn't have a collider or the collider is not set to be a trigger (make sure the collider is a collider2D!!!)
    - Neither of the objects colliding is a rigidbody (at least 1 of the objects need a Rigidbody2D component attached to it!!!)
     
    Last edited: May 9, 2014
  3. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    I think you need to convert the Collider to a GameObject, then to a transform:

    Code (csharp):
    1. GameObject gHit = other.gameObject;
    2. Transform tHit = gHit.transform;
    3. Vector3 position = new Vector3(tHit.position);
     
  4. Gistiv

    Gistiv

    Joined:
    Apr 22, 2014
    Posts:
    13
    Yes it gets executed i tried it with the your debug.log and i also got a boolian in the function which is set to true when the player enters the collider.
     
  5. Gistiv

    Gistiv

    Joined:
    Apr 22, 2014
    Posts:
    13
    @ polytropoi I tried it but it dind't work -.-'

    Code (csharp):
    1.  
    2.             GameObject gHit = other.gameObject;
    3.             Transform tHit = gHit.transform;
    4.             Enemyposition = new Vector3(tHit.position.x,
    5.                                         tHit.position.y,
    6.                                         tHit.position.z
    7.                                         );
    8.  
     
  6. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    Can you tell me what the result is of Enemyposition? is it null? is it a wrong position,...?
     
  7. Gistiv

    Gistiv

    Joined:
    Apr 22, 2014
    Posts:
    13
    I've found the problem....
    I've messed something else up in the code....
    thanks for the answers they're all working!!

    @DSSiege11 it was Vector3 with (0,0,2.67)
     
  8. DucaDiMonteSberna

    DucaDiMonteSberna

    Joined:
    Jan 18, 2018
    Posts:
    47
    other.transform.position
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    :) When you're new, it's easy to miss these things.. but this thread is 4 years old. ;)