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

Problem with trigger and setting new position for object

Discussion in 'Scripting' started by pKallv, Dec 16, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I have an object that has both Collider2D and Rigidbody2D attached. I am able to recognize what i need to recognize in regards to the trigger piece. What i want is to move objects that is dragged onto the collider-object to be moved to Vector3(0f, 0f, 0f).

    I have tested all kinds of different ways to achieve this but not succeeded. If i place a button on the scene with the same reference with the new position it works but not when i try to do it automatically.

    I have tested many different ways with limited success.

    Code (csharp):
    1.  
    2. void Update () {
    3.  
    4.     if (stopCard == false) {
    5.  
    6.           if (checkPosGameObject != null) {
    7.               print ("stopCard == false: " + checkPosGameObject.tag);
    8.               checkPosGameObject.transform.position = newVector3(0f, 0f, 0f);
    9.               print ("The pos: " + checkPosGameObject.transform.position);
    10.               stopCard = true;
    11.  
    12.         }
    13.     }
    14. }
    15.  
    16. void OnTriggerEnter2D(Collider2D col) {
    17.      print ("OnTriggerEnter2D");
    18.      checkPosGameObject = GameObject.FindWithTag (col.tag);
    19.      print (checkPosGameObject.tag);
    20.      stopCard = false;
    21. }
    22.  
    23. //The print output:
    24. OnTriggerEnter2D
    25. HAB
    26. stopCard == false: HAB
    27. The pos: (0.0, 0.0, 0.0)
    28.  
    I have tried to put the position in places like OnTriggerEnter2D and others.
    I guess the problem may be in how i collect the object.
     
    Last edited: Dec 16, 2014
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    FindWithTag(col.Tag) may not give you the object that collided if there are more objects with that tag. The collider should have inherited a gameobject so you can just use that:
    checkPosGameObject = col.gameobject;
    This would be in OnTriggerEnter
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Thanks for your answer. tested this already, and tested again, no difference i am afraid.
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I looked at your code. It probably should be opposite:
    col.gameobject.transform.position = newVector3(0f, 0f, 0f);
     
  5. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Does not work. I am currently stripping all code and restart and was thinking about if it is due to the fact that i am dragging the object past the OnTriggerEnter2D and drop it inside. I guess it is triggered before i drop it.