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 Wittz8 · Jan 29, 2020 at 07:50 PM · scripting problemarrayindexoutofrangeexception

IndexOutOfRange Issue

When I enter play mode, I keep getting the following error in the console:

IndexOutOfRangeException: Index was outside the bounds of the array. Grids2D.Sprites.PlaceSprites () (at Assets/Sprites.cs:90) Grids2D.Sprites.Start () (at Assets/Sprites.cs:57)

Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Grids2D;
 using System;
 using Random = UnityEngine.Random;
 
 namespace Grids2D {
     public class Sprites : MonoBehaviour
     {
         public GameObject redBall;
         public Grid2D grid;
         private float[] xSpriteCoords;
         private float[] ySpriteCoords;
         private int numberOfSprites = 5000;
 
         TERRAIN[] terrainAssignments;
         public enum TERRAIN
         {
             Open = 0,
             LightWater = 1,
             LightWater_Road = 2,
             HeavyWater = 3,
             HeavyWater_Road = 4,
             LightWood = 5,
             LightWood_Road = 6,
             HeavyWood = 7,
             HeavyWood_Road = 8,
             Village = 9,
             Road = 10,
             LightHill = 11,
             LightHill_Road = 12,
             HeavyHill = 13,
             HeavyHill_Road = 14,
             Impassable = 15,
             HeavyWater_HeavyHill = 16,
             HeavyHill_Village = 17,
             HeavyWater_Village = 18,
             LightHill_LightWood = 19,
             HeavyHill_HeavyWood = 20,
             HeavyWater_HeavyWood = 21,
             Road_Impassable = 22,
             Impassable_Impassable = 23,
             HeavyWater_Impassable = 24,
             LightWood_HeavyWood = 25
         }
 
         void Start()
         {
             Debug.Log("started");
             xSpriteCoords = RandomPointCoords();
             ySpriteCoords = RandomPointCoords();
             Debug.Log("randomized coordinate arrays");
             //Instantiate(redBall, new Vector3(xSpriteCoords[5], ySpriteCoords[5]), Quaternion.identity);
             terrainAssignments = TerrainAssignments(); //assign the values of the terrains array with AssignTerrain
             Debug.Log("generated terrain assignments");
             PlaceSprites();
             Debug.Log("placed sprites");
             Debug.Log("ended");
         }
 
         private float[] RandomPointCoords()
         {
             float[] array = new float[numberOfSprites];
             for (int i = 0; i < numberOfSprites; i++)
             {
                 array[i] = UnityEngine.Random.Range(-250f, 250f);
             }
             return array;
         }
 
         private TERRAIN[] TerrainAssignments() { //returns an array of terrain assignments corresponding to ammount of cells
             TERRAIN[] array = new TERRAIN[grid.numCells];
             Debug.Log(array.Length);
             for (int i = 0; i < grid.numCells; i++)
             {
                 array[i] = (TERRAIN)Random.Range(0, 25); //sets terrain type to something completely random
             }
             return array;
         }
 
         private void PlaceSprites()
         {
             TERRAIN terrain;
             for (int i = 0; i < numberOfSprites; i++)
             {
                 Debug.Log(i);
                 Debug.Log("terrainAssignments.Length = " + terrainAssignments.Length);
                 Debug.Log("xSpriteCoords.Length = " + xSpriteCoords.Length);
                 terrain = terrainAssignments[grid.CellGetIndex(grid.CellGetAtPosition(new Vector3(xSpriteCoords[i], ySpriteCoords[i])))]; //gets what terrain is at the random coordinate
                 if ((terrain == TERRAIN.HeavyWood) | (terrain == TERRAIN.HeavyWood_Road) | (terrain == TERRAIN.LightWood_HeavyWood)) //if it is heavy wood
                 {
                     Instantiate(redBall, new Vector3(xSpriteCoords[i], ySpriteCoords[i]), Quaternion.identity);
                     //Debug.Log("instantiated sprite");
                 }
             }
         }
 
     }
 }
 

According to my math, no index should be outside the bounds of the array at terrainAssignments array. Could someone help point out what I'm missing? If it helps, numCells is 30.

Thank you for your time.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Jan 29, 2020 at 08:20 PM

This line is where it goes out of index: terrain = terrainAssignments[grid.CellGetIndex(grid.CellGetAtPosition(new Vector3(xSpriteCoords[i], ySpriteCoords[i])))]; I think this is causing the issue tho.. for (int i = 0; i < numberOfSprites; i++), your loop runs 5000 times but your grid is 30x30 or 900 tiles..

Comment
Add comment · Show 2 · Share
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 Wittz8 · Jan 30, 2020 at 01:30 AM 0
Share

There are only 30 tiles, but it's not the tiles it's counting -- it's the 2 spriteCoords arrays, which each should have 5000 elements, because of lines 51 and 52.

avatar image sacredgeometry Wittz8 · Jan 30, 2020 at 07:41 AM 0
Share

It sounds like you have made a simple off by one error


simple https://en.wikipedia.org/wiki/Off-by-one_error

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

226 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Index is Less Than Zero + Flawed Code 1 Answer

Vuforia enabled scripting define 0 Answers

How not to add an object to an array/list if there's already an identical one. 2 Answers

IndexOutofRangeException 1 Answer

Trying to change character skins at the push of a button. C# 1 Answer


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