- Home /
How to properly create a 2 dimensional array of an object. [C#]
No errors in mono develop, but the console gives this for line 34:
error CS0178: Invalid rank specifier: expected ,' or
]'
I'm trying to create a List that holds 2 dimensional arrays of my placeholder objects. The object class just holds information in variables.
Here is my object class:
using UnityEngine;
using System.Collections;
public class placeholder{
public static bool exists = false;
public static bool initialized = false;
public static int priority = 0;
}
Here is my entire script I'm working on; just in case. I suspect only line 34 for will be relevant however.
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];
//flooor related
int currentfloor = 0;
public int Size_Increase_Factor = 5;
List<placeholder[][]> existanceplaceholders = new List<placeholder[][]>();
//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 roomsinitialized = 0;
int priorityfactor = 1;
placeholder[][] newplaceholder = new placeholder[gridsize][gridsize] ;
existanceplaceholders.Add (newplaceholder);
creattile (starttile, starttile, 4, priorityfactor);
priorityfactor ++;
while(roomsinitialized < roomquantity){
//int[] xcue = new int[gridsize];
//int[] ycue = new int[gridsize];
for(int i = 0; i < gridsize; i++){
for(int ii = 0; ii < gridsize; i++){
if(existanceplaceholders[currentfloor][i][ii].exists == true && existanceplaceholders[currentfloor][i][ii].initialized == false && roomsinitialized < roomquantity){
creattile(i,ii,0,priorityfactor);
}
}
}
}
Debug.Log ("test");
Debug.Log ("test");
}
void creattile(int x, int y, int staticdoorcount, int priority){
existanceplaceholders[currentfloor][x][y].exists = true;
existanceplaceholders[currentfloor][x][y].initialized = true;
existanceplaceholders [currentfloor] [x] [y].priority = priority;
int adjacents = 0;
roomsinitialized ++;
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;
}
}
if ((int)Random.Range(1, 3) == 1){
adjacents = 1;
}
else if((int)Random.Range(1, 3) != 1){
adjacents = 2;
}
else{
adjacents = 3;
}
int offsetx = 0;
int offsety = 0;
if(adjacents == 1){
int attempts = 0;
offsetx = setoffsetx();
offsety = setoffsety(offsetx);
while(existanceplaceholders[currentfloor][x +offsetx][y+offsety].exists == true){
offsetx = 0;
offsety = 0;
offsetx = setoffsetx();
offsety = setoffsety(offsetx);
attempts ++;
if(attempts > 3){
break;
}
}
existanceplaceholders[currentfloor][x +offsetx][y + offsety].exists = true;
}
else if(adjacents == 2){
for(int i=0; i<2; i++){
int attempts = 0;
offsetx = setoffsetx();
offsety = setoffsety(offsetx);
while(existanceplaceholders[currentfloor][x +offsetx][y+offsety].exists == true){
offsetx = 0;
offsety = 0;
offsetx = setoffsetx();
offsety = setoffsety(offsetx);
attempts ++;
if(attempts > 3){
break;
}
}
existanceplaceholders[currentfloor][x +offsetx][y + offsety].exists = true;
}
}
else if(adjacents == 3){
for(int i=0; i<3; i++){
int attempts = 0;
offsetx = setoffsetx();
offsety = setoffsety(offsetx);
while(existanceplaceholders[currentfloor][x +offsetx][y+offsety].exists == true){
offsetx = 0;
offsety = 0;
offsetx = setoffsetx();
offsety = setoffsety(offsetx);
attempts ++;
if(attempts > 3){
break;
}
}
existanceplaceholders[currentfloor][x +offsetx][y + offsety].exists = true;
}
}
}
int setoffsetx(){
if(Random.Range(1,2) == 1){
int rnd = Random.Range (1, 2);
if(rnd == 1){
return -1;
}
if (rnd == 2) {
return 1;
}
}
else{
return 0;
}
}
int setoffsety(int offsetx){
if(offsetx == 0){
int rnd = Random.Range (1, 2);
if(rnd == 1){
return -1;
}
if (rnd == 2) {
return 1;
}
}
else{
return 0;
}
}
void Update () {
}
}
Comment
Best Answer
Answer by Baste · Sep 26, 2014 at 08:39 PM
Check the official docs on multidimensional arrays. C#, unlike most other languages, uses a ,-notation to specify dimensions. So your line 34 should be:
placeholder[,] newplaceholder = new placeholder[gridsize,gridsize]