Noobie Question, How do I teleport an object's x position + 100

When an object collides with the object my script is attached to I want to teleport it +100 x position if it has the tag “popcorn”. This is the code I have that does not function.

    private void OnTriggerEnter2D(Collider2D col) {
        GameObject pcorn = col.gameObject;
        if (pcorn.tag == "Popcorn") {
            Vector2 newPos = col.transform.position;
            pcorn.transform.position = new Vector2(newPos.x + 100, newPos.y);           
        }
        else {
            DestroyObject(col.gameObject);
        }
    }

this also doesnt work ><

    private void OnTriggerEnter2D(Collider2D col) {
        GameObject pcorn = col.gameObject;
        if (pcorn.tag == "Popcorn") {
            Vector2 newPos = new Vector2 (col.transform.position.x +100, col.transform.position.y);
            pcorn.transform.position = newPos;           
        }
        else {
            DestroyObject(col.gameObject);
        }
    }

Code seems fine. Are you sure the TriggerEnter callback is being called? Add a Debug.Log() in there and make sure all the values are correct.

I added print(newPos); and the coordinates come out right when the collision takes place, is that what you mean?

    private void OnTriggerEnter2D(Collider2D col) {
        GameObject pcorn = col.gameObject;
        if (pcorn.tag == "Popcorn") {
            Vector2 newPos = new Vector2 (col.transform.position.x +100f, col.transform.position.y);
            pcorn.transform.position = new Vector2 (newPos.x,newPos.y);
            print(newPos);
        }
        else {
            DestroyObject(col.gameObject);
        }
    }

You add 100 to “col.transform.position”
That’s the position of Collider2D. I think you meant “pcorn.transform.position.”

FYI, there is also the Vector3.Translate method. :wink: