Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 W1k3 · Oct 16, 2014 at 04:33 AM · c#arrayobjectlist

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[i], ycue[i], 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;
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GameVortex · Oct 16, 2014 at 08:14 AM

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++)
         {
         
         }
     }
 }
Comment
Add comment · 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
1

Answer by zharik86 · Oct 16, 2014 at 07:46 AM

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.

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

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

29 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

Related Questions

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

Can't Activate a GameObject from Array 1 Answer

Instantiate a prefab from jsonarray? 1 Answer

Can´t instantiate objects in list correctly 1 Answer

How to send/copy array from dictionary to class and further to list of class 0 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