Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Jan 08, 2018 at 08:51 PM 0
Share

Which line does it say is out of range?

edit: What are the values of fieldWidth, fieldHeight in your inspector?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Has anyone seen this error before: IndexOutOfRangeException: Array index is out of range. UnityEditorInternal.... 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges