Help us understand the problem. What is going on with this article?

[Unity]オブジェクトの座標の取得と変更

More than 3 years have passed since last update.

座標取得しようとしたら、うまくいかなかったのでその方法をメモ

環境

・Unity 5.4.0f3

コード

間違ってるコード
GameObject.Find("hogehoge").transform.position.x += 100;

こんな感じで、シーンからhogehogeオブジェクトを探して、そのx座標に100を加算したかった。
でも、これだとエラーが出たのでNG

正解のコード
Vector3 tmp = GameObject.Find("hogehoge").transform.position;
GameObject.Find("hogehoge").transform.position = new Vector3(tmp.x + 100, tmp.y, tmp.z);

うーん面倒臭い。
一度座標を取り出してから、再度代入という形にしないといけないみたい。

個別の座標を取るには、

float x = tmp.x;
float y = tmp.y;
float z = tmp.z;

でできる。

haifuri
情報系の院生です
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした