LoginSignup
4

More than 1 year has passed since last update.

posted at

updated at

[Unity]特定の階層にオブジェクト生成(ラクな方法)(Unity5.4)

方法

方法としては親を指定するだけでいいのだが、生成した後にわざわざ、~transform.parent=
とかしなくてもよかったとさっき知った。

http://kurihara-n.hatenablog.com/entry/2016/08/13/011728
↑この記事を見て便利な書き方を知った。

今までInstantiate(Obj,transform.position,Quaternion.identity);
みたいに書いたあとに、~transform.parent=
ってやってたけど、Instantiate関数の第四引数にオブジェクト生成する際の親を指定できる。

Unityの公式ページ見たら確かに第四引数にも指定できることが書いてあった。(今まで読んできた本には第四引数使ってるの見たことなかったなぁ、、、(笑))
https://docs.unity3d.com/ja/2018.4/ScriptReference/Object.Instantiate.html

さっきの記事を見て、自分なりにも書いてみた。

I_am_hentai.cs
    [SerializeField]
    GameObject characterPre;//生成するもの
    GameObject character;
    [SerializeField]
    Transform characterParent;//親

    void Hoge(){
       character=(GameObject)Instantiate(characterPre,transform.position,Quaternion.identity,characterParent);
    }

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
4