• 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 W1k3 · Sep 26, 2014 at 08:26 PM · c#arrayobjectlist

How to properly create a 2 dimensional array of an object. [C#]

No errors in mono develop, but the console gives t$$anonymous$$s 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 ++;
 
         w$$anonymous$$le(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);
 
             w$$anonymous$$le(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);
                 
                 w$$anonymous$$le(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);
                 
                 w$$anonymous$$le(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
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
2
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] 


Comment
Add comment · Show 1 · 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 Baste · Sep 26, 2014 at 08:40 PM 1
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

properly initializing my arrays (C#) 2 Answers

foreach a array inside a generic list 1 Answer

Filling array with Scribtable Objects automaticly? 1 Answer

Can't Activate a GameObject from Array 1 Answer

[C#] Sorting a List of Gameobjects Alphabetically 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