Possible initialized jagged array error C#

I am using a Jagged array of GameObjects for tiled map generation, and I keep getting the error:

IndexOutOfRangeException: Array index is out of range. (wrapper stelemref) object:stelemref (object,intptr,object) Map.GenerateMap () (at Assets/Scripts/Map.cs:148) Map.Start () (at Assets/Scripts/Map.cs:74)

I am completely stumped as to why I am getting this error. Below is the full function I have written with the initialization of the jagged array. As far as I know I am initializing the array correctly.

void GenerateMap(){ mapWidth = Random.Range (5, 25); mapHeight = Random.Range (5, 25); //Initialise the Jagged Array genMapArray = new GameObject [mapHeight][]; for (int i = 0; i < mapHeight; i++) genMapArray [i] = new GameObject [mapWidth]; //Generate some mountains int mountX = Random.Range(1, mapWidth - 1); // Random within int mountY = Random.Range(1, mapHeight - 1); // the map border for (int y = 0; y < mapHeight+1; y++) { for (int x = 0; x < mapWidth+1; x++) { if((y == 0 && x <= mapWidth) || (y <= mapHeight & x == 0) || (y== mapHeight && x <= mapWidth) || (y <= mapHeight && x == mapWidth)){ genMapArray[y][x] = Tiles[0]; }else{ if(y == mountY && x == mountX){ genMapArray[y][x] = Tiles[3]; }else{ genMapArray[y][x] = Tiles[1]; } } } } //Draw Tiles on the Screen for (int y = 0; y < mapHeight+1; y++) { for (int x = 0; x < mapWidth+1; x++) { if (useSquareTiles == true && useHexagonTiles == false) { GameObject.Instantiate (genMapArray[y][x], new Vector3 (x, mapHeight - y, 0), Quaternion.Euler (-90, 0, 0)); } if (useHexagonTiles == true && useSquareTiles == false) { float hexWidth = 0.75f / Mathf.Cos (Mathf.Deg2Rad * 30.0f); GameObject.Instantiate (genMapArray[y][x], new Vector3 (x * hexWidth, y + (0.5f * Mathf.Abs (x) % 1), 0), Quaternion.Euler (-90, 0, 0)); } } } }

Category:c# Views:2 Time:2018-05-11
Tags: arrays unity3d

Related post

  • Initializing jagged arrays 2009-11-15

    I want to create array 10 * 10 * 10 in C# like int[][][] (not int[,,]). I can write code: int[][][] count = new int[10][][]; for (int i = 0; i < 10; i++) { count[i] = new int[10][]; for (int j = 0; j < 10; j++) count[i][j] = new int[10]; } but

  • How can I use a jagged array to record the x and y axes of these tiles? 2012-12-13

    In the game I am making, I have a level that is created through an XML file. The level is drawn with a nested for loop. I am trying to make another nested for loop to take the position of each tile and assign it to a jagged array. I want to do this s

  • CUDA Runtime error when copying to element of jagged array 2011-09-21

    On the host I have a jagged array implemented with a vector of vector of ints. To set up a jagged array on device, I started by allocating a pointer to a pointer of ints: int ** adjlist; // host pointer int ** d_adjlist; // device pointer Just to cla

  • How to initialize an array of arrays (or jagged array) using CodeDom? 2009-08-07

    I'm trying to use CodeDom to generate C# (.Net 2.0) code that would do the following: int[][] myArray = new int[someSize][]; In CodeDom, initializing an array requires a CodeArrayCreateExpression. The MSDN says: If a language allows arrays of arrays,

  • Initializing multidimentional arrays in c# (with other arrays) 2011-08-26

    In C#, it's possible to initialize a multidimentional array using constants like so: Object[,] twodArray = new Object[,] { {"00", "01", "02"}, {"10", "11", "12"}, {"20", "21", "22"} }; I personally think initializing an array with hard coded constant

  • Converting from a jagged array to double pointer in C# 2009-05-20

    Simple question here: is there any way to convert from a jagged array to a double pointer? e.g. Convert a double[][] to double** This can't be done just by casting unfortunately (as it can in plain old C), unfortunately. Using a fixed statement doesn

  • Do jagged arrays exist in C/C++? 2009-07-05

    Is there such a thing as a jagged array in C or C++? When I compile this: int jagged[][] = { {0,1}, {1,2,3} }; I get this error: error: declaration of `jagged' as multidimensional array must have bounds for all dimensions except the first -----------

  • Receive a Jagged Array in VBA (VB6) 2010-03-01

    I currently have a VB.NET dll that returns a jagged array of double. This is the declaration: Public Function CalcMatching(ByRef dataArray1 As Object, ByRef dataLen1 As Integer, ByRef dataArray2 As Object, ByRef dataLen2 As Integer, ByRef matchingTyp

  • Faster way to iterate through a jagged array? 2010-03-29

    I would like to iterate through an array that covers every pixel on my screen. i.e: for (int y = 598; y > 0; y--) { for (int x = 798; x > 0; x--) { if (grains[x][y]) { spriteBatch.Draw(Grain, new Vector2(x,y), Color.White); } } } ...my texture

  • How to find unique values in jagged array 2010-04-04

    I would like to know how I can count the number of unique values in a jagged array. My domain object contains a string property that has space delimitered values. class MyObject { string MyProperty; //e.g = "v1 v2 v3" } Given a list of MyObject's how

  • What is a jagged array? 2010-04-05

    What is a jagged array (in c#)? Any examples and when should one use it.... --------------Solutions------------- A jagged array is an array of arrays. string[][] arrays = new string[5][]; That's a collection of five different string arrays, each coul

  • jagged arrays multidimensional arrays conversion in ASP.NET 2010-06-09

    I would like some help to create the following convertions: A need to convert an 800*600 multidimensional array into a jagged array and then the same method in reverse (jagged array with the same data to the original multidimensional array) Is this p

  • Jagged Array and Flatten Array, Which Performs Better? 2010-10-26

    We all know that jagged array performs better than multi-dimensional array, but what about jagged array and flatten array? My intuition is that they should perform about the same. This is because both are direct access, unlike multi-dimensional array

  • Fixed statement with jagged array 2010-10-27

    I have jagged array which I need to pass to external method. [DllImport(...)] private static extern int NativeMethod(IntPtr[] ptrArray); ... fixed (ulong* ptr = array[0]) { for (int i = 0; i < array.Length; i++) { fixed (ulong* p = &array[i][0

  • PHP: What's the difference between initializing an array with "new" vs without it? 2010-11-16

    I've always created arrays by just populating them $foo[] = 'car'; but I've seen a lot of $foo = array(); $foo[] = 'car'; and $foo = new array(); What's the difference between not initializing, using array(), and using new array();? thanks! ---------

  • Why we have both jagged array and multidimentional array? 2011-01-10

    Well this may be very basic question (I feel like killing myself)... But What is the difference between jagged array and Multidimensional array. Is there a benefit of one on another? And why would the Visual Studio not allow me to do a MyClass[][] ab

  • Jagged Array in C (3D) 2011-01-17

    How could I do the following? double layer1[][3] = { {0.1,0.1,0.8}, {0.1,0.1,0.8}, {0.1,0.1,0.8}, {0.1,0.1,0.8} }; double layer2[][5] = { {0.1,0.1,0.1,0.1,0.8} }; double *upper[] = {layer1, layer2}; I read the following after trying different ideas;

  • Create couples of words in every possible combination from array 2011-03-02

    My mind is a bit numb at the moment, suppose i have this array: Array ( [0] => foo [1] => bar [2] => love ) I'd like to create couples of possible combinations (minus one val), such as foo bar bar love foo love and not their opposite (but th

  • Add item to a Jagged array 2011-03-21

    I have already looked at some array topics but I am still stumped. I wish to add a new line to my jagged array - and am strugigng to get the syntax right.. int[][] intJaggedArray = new int[7][]; intJaggedArray[0] = new int[3] { 1, 1, 1 }; intJaggedAr

  • Return a jagged array with dynamic number of dimensions from C# WCF Service 2011-04-12

    I'm currently trying create a service that will return results of a OLAP cube query in a C#/ WCF data service. I am doing this to gain complete programmatic control over how the OLAP results are serialized, how clients are authenticated/authorized an

  • malloc 2D array for CG Rects, initializing with array notation 2011-04-15

    int rows = 2; int columns = 2; positions = malloc(rows * sizeof(CGRect)); for(int i=0; i<rows; i++){ positions[i] = malloc(columns * sizeof(CGRect)); } positions[0][0] = CGRectMake(0, 0, 1, 1); positions[0][1] = CGRectMake(1, 1, 2, 2); positions[0

  • How to initialize a jagged array in JavaScript? 2011-05-09

    Is it possible to have a jagged array in JavaScript? Here is the format of the data I want to store in a jagged array: (key)(value1, value2, value3) Can I put this in a jagged array? --------------Solutions------------- Yes, you can create that type

  • fill 3-dimensional array(jagged array) with point data 2011-05-23

    I do have a 3-dimensional matrix private int[][][] Matrix but I dont know how to fill this. the first dimension is for my slices of a picture the second for my x-values of one slice ad the 3rd slice for my y-values. so das anybody know how to fill th

  • Problem Marshalling C# Jagged Array to C++ 2011-06-13

    I'm trying to marshal a 2D C# jagged array (double[][] jaggedArray) to a C++ dll where i've specified the receiving variable to be a double**. However, i'm getting the message: There is no marshaling support for nested arrays. Short of flattening the

  • Add Item to Jagged Array 2011-06-23

    It is a homework. I want to store names and total votes of electoral candidates for country provinces. In this practice I should use arrays. Since the number of candidates is not known I thinks I should use Jagged arrays. But number of provinces is k

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.138 (s). 11 q(s)