- Home /
Single GameObject showing two different positions/localPositions
So I dug around quite a bit but I'm like .. really confused. I think this might have something to do with my rb2d configuration or something. But what I have is really simple:
I have NO idea how this one game object, depending on what script you are referencing it from can have two different positions and localPositions, but I haven't used unity in quite a while and I might be forgetting something really really simple here. I'm almost positive it's simple. But looking at the object hierarchy, the scripts, and I can't find any reason for this to be the case. The only reason I think it might have something to do with my rb2d is because in the game manager script, it's impossible for me to move the game object around with transform.position, it just always stays static from the time of game start. (It starts in the hierarchy, it's not instantiated later)
First I have a simple GameObject with a sprite renderer and a rb2d, and it has a script attached to it called Player.
public class Player : MonoBehaviour
{
void OnMouseDown()
{
Debug.Log(transform.position);
Debug.Log(transform.localPosition);
Debug.Log(GetComponent<Rigidbody2D>().transform.position);
Debug.Log(GetComponent<Rigidbody2D>().transform.localPosition);
}
}
I have a singleton game object which runs some level generation, very simple, which I've set the position of the the Player game object with, which also for the purposes of this test spams out its transform position and local position.
public GameObject player;
void Start(){
// ...
player.transform.position = new Vector3(-10, -10, 0f);
}
void Update(){
Debug.Log(player.transform.position);
Debug.Log(player.transform.localPosition);
}
In this screenshot you can see the output of those Logs, as well as the position in the inspector of the player game object.
Here is screenshot of output from player script:
Here is Rb2d properties
Any help would be hugely appreciated thanks for taking the time to read this huge post.
Answer by luuhoang231 · Jun 20, 2020 at 02:03 AM
where are you get the ref player gameobject you should put it in awake method. If your gameManager is prefabs you cant put your gameObject in scene to
public GameObject player;
void Awake() {
player = GameObject.Find("Player");
}
void Start(){
// ...
player.transform.position = new Vector3(-10, -10, 0f);
}
void Update(){
Debug.Log(player.transform.position);
Debug.Log(player.transform.localPosition);
}
I see so I am referencing a prefab which is different than the instantiated object in the hierarchy.
Thank you so much!
Your answer
![](https://koobas.hobune.stream/wayback/20220613001344im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get instantiated prefab position from Collision Trigger 0 Answers
Camera rotation around player while following. 6 Answers
transform.position not moving object 2 Answers
C# can i set rigidbody2d to gravitate to point x but not y 0 Answers
Need insight on strange (simple) 2D object transform bug 0 Answers