LoginSignup
2
2

More than 3 years have passed since last update.

『Unity C# 個人用メモ』 アイテムの一括生成と一括削除

Last updated at Posted at 2020-02-21
public class Seisei : MonoBehaviour
{
    //生成したいオブジェクトを宣言
    [SerializeField] GameObject obj = default;

    void Update()
    {
        //一括生成
        if (Input.GetMouseButtonDown(0))
        {
            //x座標用
            for (int xi = 0; xi < 5; xi++)
            {
                //y座標用
                for (int zi = 0; zi < 3; zi++)
                {
                    //生成(対象,座標,角度)
                    Instantiate(obj, new Vector3(xi, 1f, zi), Quaternion.identity);
                }
            }
        }
    //一括削除
    if (Input.GetMouseButtonDown(1))
    {
        //あらかじめオブジェクトにタグをつけておいて、そのタグのついたオブジェクトをまとめて配列にぶち込む
        GameObject[] objs = GameObject.FindGameObjectsWithTag("obj");

        //配列onjsにぶち込んだものをを一纏めにする
        foreach(GameObject clone in objs)
        {
            Destroy(clone);
        }
    }
}

}

2
2
0

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
  3. You can use dark theme
What you can do with signing up
2
2