How to change position of gameobject
How to change postition of gameobjects? (using C#)
I need to teleport a gameobject to another location.
I'm aware of this "transform.position", but I don't know how to specify which object it should refer to.,Can someone tell me how to change position of gameobjects? (using C#)
I need to teleport a gameobject to another location.
Answer by LeeJBaxter · Feb 21, 2018 at 01:36 PM
Simple!!!
// You'll need to set this either within the script or within the inspector.
GameObject gameObjectToMove;
public void MoveGameObject()
{
gameObjectToMove.transform.position = new Vector3(x, y, z);
}
Using "transform.position" by itself is short for saying "this.transform.position", in which case you're simply referring to the game-object that the script is attached to.
Answer by xxshadowfire · Jan 24, 2020 at 01:11 AM
when i try doing this it just moves it to a different position to what i put in heres my code
if (Input.GetKeyDown(KeyCode.Space)) { FishAnim.GetComponent().Play("PunkFishInvincible"); Speed += InvincibleSpeed; FishAnim.GetComponent().offset = new Vector2(7, -0.6f);
FishName.transform.position = new Vector2(300, -150);
}
else if (Input.GetKeyUp(KeyCode.Space))
{
FishAnim.GetComponent<Animator>().Play("PunkFishIdle");
Speed = 5;
FishAnim.GetComponent<BoxCollider2D>().offset = new Vector2(0f, -0.6f);
FishName.transform.position = new Vector2(0,-150);
Your answer
![](https://koobas.hobune.stream/wayback/20220612152958im_/https://answers.unity.com/themes/thub/images/avi.jpg)