How to Teleport Player to Instantiated Object Position

I’m trying to achieve a teleport effect.
I want to instantiate a Sword at a “fire point” in front of the player when they press a button. And then when they press another button, the player should teleport to where the Sword currently sits.
I have this at the beginning of my script.

public GameObject swordObject;
public Transform firePoint;

So when I press a button, it calls the first method:

GameObject swordObj = Instantiate(swordObject, firePoint.transform.position, Quaternion.identity);

And then when I press another button, it calls the second method:
(This is where the teleport should happen).

I separated the methods so that I could call them at specific times during an animation.
How would I be able to reference the instantiated Sword’s position in the second method so that I can teleport my player there? I just need to be able to reference the position of the instantiated object from a different method. Thanks.

Perhaps keep track of it in a member variable instead of a local variable, then the other function can have at its position when it needs it.

How would I be able to set the instantiated Sword object as a member variable?

A member variable is one declared outside of a function, just in class scope. Your swordObj above is a local variable (because you declare it there as a GameObject) so nobody else can get at it.

Okay thank you! I got it working.

1 Like

can you gimme da example please , i still dont understand

Hi, which part did you still need help with?

actualy idid fixed m,thanks