properly initializing my arrays (C#)

I have a list that holds 2 dimensional arrays. When I try to accesses one of my objects, it’s out of range. setting all indexes to 0 yields the same result so I can only assume something isn’t initialized properly. line 21, and line 41-47 are my initialization.

Here is the start of my code. the error is on line 74, where I have temporarily set all indexes to 0.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class floorbuilderv2 : MonoBehaviour {

	//room related
	public GameObject[] fourrooms = new GameObject[3];
	public GameObject[] threerooms = new GameObject[3];
	public GameObject[] tworooms = new GameObject[3];
	public GameObject[] onerooms = new GameObject[3];
	public GameObject[] startrooms = new GameObject[1];

	//settings
	public GameObject TestRepresent;
	public float distance = 10f;

	//flooor related
	int currentfloor = 0;
	public int Size_Increase_Factor = 5;
	List<placeholder[,]> existanceplaceholders = new List<placeholder[,]>();
	int roomcount = 0;
	int initialisations = 0; 
 
	//placeholder[][] existance = new placeholder[10][10]; 

	//misc
	void Start () {
		transform.position = new Vector3 (0, 0, 0);
		buildnextfloor ();
	}
	
	void buildnextfloor(){
		int gridsize = currentfloor * 10;
		int starttile = Mathf.FloorToInt (gridsize/2);
		int roomquantity = (currentfloor + currentfloor) * Size_Increase_Factor;
		int priorityfactor = 1;
		roomcount = 0;
		initialisations = 0;

		existanceplaceholders.Add(new placeholder[gridsize,gridsize]); 

		for (int i=0; i < (gridsize); i++) {
			for(int ii =0; i < gridsize; i++){
				existanceplaceholders[currentfloor][i, ii] = new placeholder();
			}
		}

		////////////////////////////////////////////////////////////////////////////////

		creattile (starttile, starttile, 4, priorityfactor, gridsize);
		priorityfactor ++;

		while(true){
			int[] xcue = findnextcue(gridsize, true);
			int[] ycue = findnextcue(gridsize, false);

			for(int i =0; i < initialisations; i++){
				if(roomcount < roomquantity){
					creattile(xcue_, ycue*, 0, priorityfactor, gridsize);*_

* }*
* else{*
* break;*
* }*
* }*
* if(roomcount >= roomquantity){*
* break;*
* }*
* }*
* buildnextfloorfaze2(gridsize);*
* }*

* void creattile(int x, int y, int staticdoorcount, int priority, int gridsize){*
* existanceplaceholders[0][0, 0].exists = true;*
* existanceplaceholders[currentfloor][x, y].initialised = true;*
* existanceplaceholders [currentfloor] [x, y].location = generatecoordinates (x, y, gridsize);*
* roomcount ++;*
* existanceplaceholders [currentfloor] [x, y].priority = priority;*
* int adjacents = 0;*

* if (staticdoorcount > 0){*
* if(staticdoorcount == 4){*
* existanceplaceholders[currentfloor][x+1, y].exists = true;*
* existanceplaceholders[currentfloor][x, y+1].exists = true;*
* existanceplaceholders[currentfloor][x-1 ,y].exists = true;*
* existanceplaceholders[currentfloor][x, y-1].exists = true;*
* }*
* }*
This is the code for my object:
using UnityEngine;
using System.Collections;

public class placeholder{

* public string type;*
* public int priority;*
* public Vector3 location;*
* public bool exists = false;*
* public bool initialised = false;*
* public bool isphysicle = false;*
* public bool hasdoorconections = false;*
}

The first, in my opinion, is better to initialize on another a two-dimensional array. I didn’t experiment with one-dimensional, but for two-dimensional if you create new myArr[0, 0], length of an array is “-1”. And the second, it is necessary to enter check on null:

 void buildnextfloor(){
  int gridsize = currentfloor * 10; //in your case this value is 0
  int starttile = Mathf.FloorToInt (gridsize/2); //in your case this value is 0
  int roomquantity = (currentfloor + currentfloor) * Size_Increase_Factor;
  int priorityfactor = 1;
  roomcount = 0;
  initialisations = 0;

  //Other initialization
  placeholder[ , ] tempic = null; //first, reference is null
  if(gridsize > 0) {
   tempic = new placeholder[gridsize,gridsize];
  }
  existanceplaceholders.Add(tempic); 
  //initialization tempic
  if(tempic != null) {
   for (int i=0; i < gridsize; i++) {
    for(int ii =0; i1 < gridsize; i1++){
     tempic[i, ii] = new placeholder();
    }
   }
  }
  ...
 }

And check null in your function

 void creattile(int x, int y, int staticdoorcount, int priority, int gridsize) {
  if(existanceplaceholders[currentfloor] == null) {
   return;
  }
  ...
 }

I hope that it will help you.

I can not see you that you are setting the value of the “currentFloor” variable anywhere, except for the initialization to 0. It is also private so you are not setting it in the inspector either. The “gridSize” variable is multiplied with the “currentFloor” on line 34, which means that “gridSize” will always be 0. So at line 41 you initialize the 2D array with a gridSize of 0, so the array has no elements (0 length). That is why it fails at line 74.

Also in your nested loop to populate the 2D array you are not iterating the correct variable. You have:

for(int ii =0; i < gridsize; i++)

The variable ii is never increased. You probably want to use the ii for both the check and the iteration like so:

for(int ii =0; ii < gridsize; ii++)

For nested loops by the way it is more common to use “j” as the iterator, and then “k” and so on.
Example:

for(int i = 0; i < value; i++)
{
    for(int j = 0; j < value2; j++)
    {
        for(int k = 0; k < value3; k++)
        {
        
        }
    }
}