前提・実現したいこと
エラーを解消したい
発生している問題・エラーメッセージ
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を使っているから悪いのでしょうか? 分からないので教えてもらえるとありがたいです。
どう書き換えたのか(書き換える前後の比較)がないとちゃんとした回答は期待できないと思います。
回答2件
あなたの回答
tips
プレビュー