- Home /
putting an object's x coordinate to another object
Hello: I want to know if I can get the x coordinate of an object and put it in another object. I need this because I want to put the camera aligned to the player. Thank you very much!!!
Answer by Piflik · Jan 04, 2013 at 09:58 PM
Yes, you can.
In all seriousness...you have to first get a reference to the Object, possibly via a variable of type GameObject exposed in the Inspector, or by using GameObject.FindWithTag("Player") in the Start function, and then you can access its transform, just like you can access the transform of the object the script is attached to.
Example:
private GameObject player;
void Start() {
player = GameObject.FindWithTag("Player");
}
void Update() {
transform.position = player.transform.position;
}
Have a look here for an overview on how to access other GameObjects.
but I think that's for modifying things of another object. I want to put the object's coordinates in a var, and then use it like this: transform.position.x = var; thank you!!!