質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2499閲覧

Unity Listに全ての要素を追加出来ない

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2018/07/19 18:34

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AI2 : MonoBehaviour { 6 7 public float timeOut = 3; 8 private Vector3 Cre_Pos1; 9 private Vector3 Cre_Pos2; 10 private Vector3 Cre_Pos3; 11 private Vector3 Cre_Pos4; 12 private Vector3 Cre_Pos5; 13 private Vector3 Cre_Pos6; 14 private Vector3 Cre_Pos7; 15 private Vector3 Cre_Pos8; 16 private List<GameObject> TmpList; 17 18 // Use this for initialization 19 void Start () { 20 StartCoroutine(Move()); 21 } 22 23 // Update is called once per frame 24 void Update () { 25 26 } 27 28 IEnumerator Move() 29 { 30 while (true) 31 { 32 Vector3 Pos = transform.position; 33 Vector3 Pos1 = new Vector3(Pos.x - 16, Pos.y + 16, Pos.z); 34 Vector3 Pos2 = new Vector3(Pos.x - 16, Pos.y, Pos.z); 35 Vector3 Pos3 = new Vector3(Pos.x - 16, Pos.y - 16, Pos.z); 36 Vector3 Pos4 = new Vector3(Pos.x, Pos.y - 16, Pos.z); 37 Vector3 Pos5 = new Vector3(Pos.x + 16, Pos.y - 16, Pos.z); 38 Vector3 Pos6 = new Vector3(Pos.x + 16, Pos.y, Pos.z); 39 Vector3 Pos7 = new Vector3(Pos.x + 16, Pos.y + 16, Pos.z); 40 Vector3 Pos8 = new Vector3(Pos.x, Pos.y + 16, Pos.z); 41 42 GameObject[] Creatures = GameObject.FindGameObjectsWithTag("Creature"); 43 var list = new List<GameObject>(); 44 list.AddRange(Creatures); 45 foreach (GameObject Cre in list) 46 { 47 TmpList = new List<GameObject>(); 48 TmpList.Add(Cre); 49 } 50 foreach (GameObject Cre in TmpList) 51 { 52 if (Cre.transform.position == Pos) 53 { 54 list.Remove(Cre); 55 } 56 if (Cre.transform.position == Pos1) 57 { 58 Cre_Pos1 = Cre.transform.position; 59 } 60 if (Cre.transform.position == Pos2) 61 { 62 Cre_Pos2 = Cre.transform.position; 63 } 64 if (Cre.transform.position == Pos3) 65 { 66 Cre_Pos3 = Cre.transform.position; 67 } 68 if (Cre.transform.position == Pos4) 69 { 70 Cre_Pos4 = Cre.transform.position; 71 } 72 if (Cre.transform.position == Pos5) 73 { 74 Cre_Pos5 = Cre.transform.position; 75 } 76 if (Cre.transform.position == Pos6) 77 { 78 Cre_Pos6 = Cre.transform.position; 79 } 80 if (Cre.transform.position == Pos7) 81 { 82 Cre_Pos7 = Cre.transform.position; 83 } 84 if (Cre.transform.position == Pos8) 85 { 86 Cre_Pos8 = Cre.transform.position; 87 } 88 } 89 yield return new WaitForSeconds(timeOut); 90 } 91 } 92} 93

TmpList.Add(Cre);でlist内の要素を追加しているのですが、一つの要素しかTmpListに追加されません。どうすれば全ての要素を追加出来るのでしょうか?
回答お願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AI2 : MonoBehaviour { 6 7 public float timeOut = 3; 8 private Vector3 Cre_Pos1; 9 private Vector3 Cre_Pos2; 10 private Vector3 Cre_Pos3; 11 private Vector3 Cre_Pos4; 12 private Vector3 Cre_Pos5; 13 private Vector3 Cre_Pos6; 14 private Vector3 Cre_Pos7; 15 private Vector3 Cre_Pos8; 16 private List<GameObject> TmpList; 17 18 // Use this for initialization 19 void Start () { 20 StartCoroutine(Move()); 21 } 22 23 // Update is called once per frame 24 void Update () { 25 26 } 27 28 IEnumerator Move() 29 { 30 while (true) 31 { 32 Vector3 Pos = transform.position; 33 Vector3 Pos1 = new Vector3(Pos.x - 16, Pos.y + 16, Pos.z); 34 Vector3 Pos2 = new Vector3(Pos.x - 16, Pos.y, Pos.z); 35 Vector3 Pos3 = new Vector3(Pos.x - 16, Pos.y - 16, Pos.z); 36 Vector3 Pos4 = new Vector3(Pos.x, Pos.y - 16, Pos.z); 37 Vector3 Pos5 = new Vector3(Pos.x + 16, Pos.y - 16, Pos.z); 38 Vector3 Pos6 = new Vector3(Pos.x + 16, Pos.y, Pos.z); 39 Vector3 Pos7 = new Vector3(Pos.x + 16, Pos.y + 16, Pos.z); 40 Vector3 Pos8 = new Vector3(Pos.x, Pos.y + 16, Pos.z); 41 42 GameObject[] Creatures = GameObject.FindGameObjectsWithTag("Creature"); 43 var list = new List<GameObject>(); 44 list.AddRange(Creatures); 45 TmpList = new List<GameObject>(); 46 47 foreach (GameObject Cre in list) 48 { 49 TmpList.Add(Cre); 50 }

で、出来ました。

投稿2018/07/19 22:00

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問