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

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

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

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

Unity

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

Q&A

解決済

2回答

1632閲覧

Unity ArgumentOutOfRangeException

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Unity

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

0グッド

1クリップ

投稿2019/06/15 16:54

前提・実現したいこと

エラーを解消したい

発生している問題・エラーメッセージ

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0) System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <d7ac571ca2d04b2f981d0d886fa067cf>:0) System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0) Astar.Sample+<_StartMove>d__14.MoveNext () (at Assets/Script/Astar/Sample.cs:117) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) Astar.Sample:StartMove() (at Assets/Script/Astar/Sample.cs:98) Astar.Sample:Goto(Vector2Int) (at Assets/Script/Astar/Sample.cs:87) Astar.Sample:Update() (at Assets/Script/Astar/Sample.cs:78)

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using DG.Tweening; 4using UnityEngine; 5 6namespace Astar 7{ 8 public class Sample : MonoBehaviour 9 { 10 [SerializeField] 11 private Camera _camera; 12 13 private Tile[,] tiles; 14 15 private Vector2Int _currentNodeId; 16 public float walkSpeed = 0.6f; 17 List<Vector2Int> _routeList = new List<Vector2Int>(); 18 19 public GameObject UnityChan; 20 21 GameObject base_; 22 MousePos mousePos; 23 24 private Tile tile; 25 26 void Start() 27 { 28 base_ = GameObject.Find("Base"); 29 mousePos = base_.GetComponent<MousePos>(); 30 MapInfo mapInfo = new MapInfo(); 31 tile = new Tile(); 32 33 tiles = new Tile[mapInfo.width, mapInfo.height]; 34 // Astar初期化 35 RouteManager.Instance.Initialize(mapInfo.width,mapInfo.height); 36 37 //int count = 0; 38 for (int x = 0; x < mapInfo.width; x++) 39 { 40 for (int y = 0; y < mapInfo.height; y++) 41 { 42 //var tile = Instantiate(tilePrefab) as Tile; 43 tiles[x, y] = tile; 44 tile.SetNodeId(new Vector2Int(x, y)); 45 46 //tile.transform.localPosition = new Vector3(x, tile.transform.localPosition.y, y); 47 } 48 } 49 50 void Update() 51 { 52 if (mousePos.OK == true) 53 { 54 Goto(mousePos.MousePos_); 55 } 56 } 57 58 void Goto(Vector2Int goalNodeId) 59 { 60 if (RouteManager.Instance.SearchRoute(_currentNodeId, goalNodeId, _routeList)) 61 { 62 // 移動 63 StartMove(); 64 } 65 66 _currentNodeId = goalNodeId; 67 } 68 69 private Coroutine _moveCoroutine; 70 71 private void StartMove() 72 { 73 if (_moveCoroutine != null) StopCoroutine(_moveCoroutine); 74 _moveCoroutine = StartCoroutine(_StartMove()); 75 } 76 77 IEnumerator _StartMove() 78 { 79 var wait = new WaitForSeconds(walkSpeed); 80 posList.Clear(); 81 posList2.Clear(); 82 for (int i = 0; i < _routeList.Count; i++) 83 { 84 var nodeId = _routeList[i]; 85 var goal = new Vector2(nodeId.x, nodeId.y); 86 posList.Add(goal); 87 posList2.Add(goal); 88 //iTween.MoveTo(UnityChan, goal, 0.5f); 89 } 90 for(int i = 0;i < posList.Count; i++) 91 { 92 var nodeId = posList[i]; 93 var nodeId2 = posList[i + 1]; 94 95 if(nodeId.y == nodeId2.y && nodeId.x < nodeId2.x) 96 { 97 posList2.RemoveAt(i); 98 } 99 } 100 for(int i = 0;i < posList2.Count; i++) 101 { 102 var nodeId = posList2[i]; 103 var goal = new Vector2(nodeId.x, nodeId.y); 104 iTween.MoveTo(UnityChan, goal, 0.5f); 105 yield return wait; 106 107 } 108 _moveCoroutine = null; 109 } 110 111 private List<Vector2> posList = new List<Vector2>(); 112 private List<Vector2> posList2 = new List<Vector2>(); 113 } 114} 115

試したこと

C#

1 for(int i = 0;i < posList2.Count; i++) 2 { 3 var nodeId = posList2[i]; 4 var goal = new Vector2(nodeId.x, nodeId.y); 5 iTween.MoveTo(UnityChan, goal, 0.5f); 6 yield return wait; 7 8 }

この部分を書き加えるとエラーになってしまいます。要素を削除したListを使っているから悪いのでしょうか? 分からないので教えてもらえるとありがたいです。

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

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

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

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

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

gentaro

2019/06/15 23:22

どう書き換えたのか(書き換える前後の比較)がないとちゃんとした回答は期待できないと思います。
guest

回答2

0

ベストアンサー

削除する要素が間違っていました。RemoveAtではなく、特定の座標をRemoveすれば、エラーは解消され、目的通りの動きをしてくれました。

c#

1using System.Collections; 2using System.Collections.Generic; 3using DG.Tweening; 4using UnityEngine; 5 6namespace Astar 7{ 8 public class Sample : MonoBehaviour 9 { 10 [SerializeField] 11 private Camera _camera; 12 13 private Tile[,] tiles; 14 15 private Vector2Int _currentNodeId; 16 public float walkSpeed = 0.6f; 17 List<Vector2Int> _routeList = new List<Vector2Int>(); 18 19 public GameObject UnityChan; 20 21 GameObject base_; 22 MousePos mousePos; 23 24 private Tile tile; 25 26 void Start() 27 { 28 base_ = GameObject.Find("Base"); 29 mousePos = base_.GetComponent<MousePos>(); 30 MapInfo mapInfo = new MapInfo(); 31 tile = new Tile(); 32 33 tiles = new Tile[mapInfo.width, mapInfo.height]; 34 // Astar初期化 35 RouteManager.Instance.Initialize(mapInfo.width,mapInfo.height); 36 37 //int count = 0; 38 for (int x = 0; x < mapInfo.width; x++) 39 { 40 for (int y = 0; y < mapInfo.height; y++) 41 { 42 tiles[x, y] = tile; 43 tile.SetNodeId(new Vector2Int(x, y)); 44 } 45 } 46 } 47 48 void Update() 49 { 50 if (mousePos.OK == true) 51 { 52 Goto(mousePos.MousePos_); 53 } 54 } 55 56 void Goto(Vector2Int goalNodeId) 57 { 58 if (RouteManager.Instance.SearchRoute(_currentNodeId, goalNodeId, _routeList)) 59 { 60 // 移動 61 StartMove(); 62 } 63 64 _currentNodeId = goalNodeId; 65 } 66 67 private Coroutine _moveCoroutine; 68 69 private void StartMove() 70 { 71 if (_moveCoroutine != null) StopCoroutine(_moveCoroutine); 72 _moveCoroutine = StartCoroutine(_StartMove()); 73 } 74 75 IEnumerator _StartMove() 76 { 77 var wait = new WaitForSeconds(walkSpeed); 78 posList.Clear(); 79 posList2.Clear(); 80 for (int i = 0; i < _routeList.Count; i++) 81 { 82 var nodeId = _routeList[i]; 83 var goal = new Vector2(nodeId.x, nodeId.y); 84 posList.Add(goal); 85 posList2.Add(goal); 86 } 87 for(int i = 0;i < posList.Count; i++) 88 { 89 if(i != posList.Count - 1) 90 { 91 var nodeId = posList[i]; 92 var nodeId2 = posList[i + 1]; 93 94 if (nodeId.y == nodeId2.y && nodeId.x < nodeId2.x) 95 { 96 posList2.Remove(nodeId); 97 } 98 } 99 } 100 foreach(Vector2 item in posList2) 101 { 102 var nodeId = item; 103 var goal = new Vector2(nodeId.x, nodeId.y); 104 iTween.MoveTo(UnityChan, goal, 0.5f); 105 yield return wait; 106 } 107 108 _moveCoroutine = null; 109 } 110 111 private List<Vector2> posList = new List<Vector2>(); 112 private List<Vector2> posList2 = new List<Vector2>(); 113 } 114} 115

投稿2019/06/16 19:54

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

まだちゃんと読んでませんが、foreachにしてみてはどうでしょう?
すくなくとも、ない要素が計算対象にされることなくなるはずです

foreach(var item in posList2) { var nodeId = item; var goal = new Vector2(nodeId.x, nodeId.y); iTween.MoveTo(UnityChan, goal, 0.5f); yield return wait; }

他のfor文も、可能な限りforeachにすべきです。foreachならば、要素がない場合は実行そのものがされなくなりますので

投稿2019/06/15 17:52

編集2019/06/15 18:00
naby

総合スコア126

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

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

退会済みユーザー

退会済みユーザー

2019/06/16 19:26

foreachにしてみましたが、同じエラーが出ました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問