- Home /
Question by
RakugakiX · Jan 08, 2018 at 08:51 PM ·
indexoutofrangeexception
IndexOutOfRangeExceptions: Array Index is out of range - Unity2D Tower Defense game,IndexOutOfRangeException: Array index is out of range - Unity2D tower defense game
I've been working on a tower defense game and am working on the waypoint system, where the console would repeat the cells of the pathway of the loaded level. This can be more seen in the bottom Loadwaypoints void. But instead of following the debug.log and posting the message on the console is just says that the array index is out of range. Help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class LvlManagerScr : MonoBehaviour {
public int fieldWidth, fieldHeight;
public GameObject cellPref;
public Transform cellParent;
public Sprite[] tileSpr = new Sprite[2];
public List<GameObject> wayPoints = new List<GameObject>();
GameObject[,] allCells = new GameObject[10, 22];
int currWayX, currWayY;
GameObject firstCell;
void Start () {
CreateLevel();
LoadWaypoints();
}
void CreateLevel()
{
Vector3 worldVec = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height, 0));
for (int i = 0; i < fieldHeight; i++)
for(int k = 0; k < fieldWidth; k++)
{
int sprIndex = int.Parse(LoadLevelText(1)[i].ToCharArray()[k].ToString());
Sprite spr = tileSpr[sprIndex];
bool isGround = spr == tileSpr[1] ? true : false;
CreateCell (isGround, spr, k, i, worldVec);
}
}
void CreateCell(bool isGround, Sprite spr, int x, int y, Vector3 wV)
{
GameObject tmpCell = Instantiate(cellPref);
tmpCell.transform.SetParent(cellParent, false);
tmpCell.GetComponent<SpriteRenderer>().sprite = spr;
float sprSizeX = tmpCell.GetComponent<SpriteRenderer>().bounds.size.x;
float sprSizeY = tmpCell.GetComponent<SpriteRenderer>().bounds.size.y;
tmpCell.transform.position = new Vector3(wV.x + (sprSizeX * x), wV.y + (sprSizeY * -y));
if (isGround)
{
tmpCell.GetComponent<CellScr>().isGround = true;
if (firstCell == null)
{
firstCell = tmpCell;
currWayX = x;
currWayY = y;
}
}
allCells[y, x] = tmpCell;
}
string[]LoadLevelText (int i)
{
TextAsset tmpTxt = Resources.Load<TextAsset> ("Level" + i + "Ground");
string tmpStr = tmpTxt.text.Replace (Environment.NewLine, string.Empty);
return tmpStr.Split ('!');
}
void LoadWaypoints()
{
GameObject currWay60;
wayPoints.Add(firstCell);
while(true)
{
currWay60 = null;
if (currWayX > 0 && allCells[currWayY, currWayX - 1].GetComponent<CellScr>().isGround &&
!wayPoints.Exists(x => x == allCells[currWayY, currWayX - 1]))
{
currWay60 = allCells[currWayY, currWayX - 1];
currWayX--;
Debug.Log ("Next Cell is Left");
}
else if (currWayX < (fieldWidth - 1) && allCells[currWayY, currWayX + 1].GetComponent<CellScr>().isGround &&
!wayPoints.Exists (x => x == allCells[currWayY, currWayX + 1]))
{
currWay60 = allCells[currWayY, currWayX + 1];
currWayX++;
Debug.Log("Next Cell is Right");
}
else if (currWayY > 0 && allCells[currWayY - 1, currWayX].GetComponent<CellScr>().isGround &&
!wayPoints.Exists(x => x == allCells[currWayY - 1, currWayX]))
{
currWay60 = allCells[currWayY - 1, currWayX];
currWayY--;
Debug.Log("Next Cell is Up");
}
else if (currWayY < (fieldHeight - 1) && allCells[currWayY + 1, currWayX].GetComponent<CellScr>().isGround &&
!wayPoints.Exists(x => x == allCells[currWayY - 1, currWayX]))
{
currWay60 = allCells[currWayY + 1, currWayX];
currWayY++;
Debug.Log("Next Cell is Down");
}
else
break;
wayPoints.Add (currWay60);
}
}
}
Comment
Which line does it say is out of range?
edit: What are the values of fieldWidth, fieldHeight in your inspector?
Your answer
Follow this Question
Related Questions
Array index is out of range 2 Answers
for loop problem 1 Answer
Index Array is out of Range 1 Answer
IndexOutOfRangeException when activating a component(follow-up question) 1 Answer