Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Lunoxx · Oct 08, 2018 at 08:41 PM · arraylist

Add an Object to a List which is part of an array does not work.

I have the problem, that i want to add objects to a list which is part of an array. i was searching in the forum, but could not find a correct solution, because mostly the problem was in the instantiating of the list and that is what i have done correct(as far as i can say):

the error comes in line 27 of the 2nd part of code.

so here is the part of my code that makes the problem:

  public class PlaygroundCreatorWorkingConcept : MonoBehaviour
  {
      public int height;
      public int width;
     
      List<WallInformation> WallInformations = new List<WallInformation>();
      List<WallInformation>[,] WallInformationSmall;
  }
  
   public class WallInformation
      {
          public Coord coordinate;
          public bool isWall;
          public float floorHeight;
          public float ceilingHeight;
  
          public WallInformation(Coord newCoordinate, bool newIsWall, float newFloorHeight, float newCeilingHeight)
          {
              coordinate = newCoordinate;
              isWall = newIsWall;
              floorHeight = newFloorHeight;
              ceilingHeight = newCeilingHeight;
          }
      }
  
  
  void Awake()
      {
     WallInformationSmall = new List<WallInformation>[width / 10, height / 10];
          for(int i = 0; i >= width / 10; i++)
          {
              for (int j = 0; j >= height / 10; j++)
              {
                  WallInformationSmall[i, j] = new List<WallInformation>();
              }
          }
       }

this is the top of the programm with all important thing for the question

   public void SetWallInformations()
      {
          for (int x = 0; x < width; x++)
          {
              for (int z = 0; z < height; z++)
              {
                  WallInformations.Add(new WallInformation(new Coord(x, z), isWall[x, z], floorWorld[x, z], ceilingWorld[x, z]));
              }
          } //scheint bis hier zu funktionieren, da er bei small 50*50 = 2500 informationen füllt.
          foreach (WallInformation WallInformation in WallInformations)
          {
              Debug.Log(WallInformations.Count);
              Debug.Log(WallInformation.coordinate.tileX);
              Debug.Log(WallInformation.coordinate.tileZ);
              Debug.Log(WallInformation.isWall);
              Debug.Log(WallInformation.floorHeight);
              Debug.Log(WallInformation.ceilingHeight);
              int i = 0;
              int j = 0;
              if (WallInformation.coordinate.tileX >= i + 0 && WallInformation.coordinate.tileX <= i + 10)
              {
                  Debug.Log("Bin Hier1.");
  
                  if (WallInformation.coordinate.tileZ >= j + 0 && WallInformation.coordinate.tileZ <= j + 10)
                  {
                      Debug.Log("Bin Hier2.");
                      WallInformationSmall[i, j].Add(WallInformation);
                  }
                  else
                  {
                      j++;
                      //return;
                      Debug.Log("Bin Hier3.");
                  }
              }
              else
              {
                  i++;
                  j = 0;
                  //return;
              }
              i = 0;
              Debug.Log("Bin Hier4.");
  
          }
     }

the problem is that the line below Debug.Log("Bin Hier2"); does give me this error:

NullReferenceException: Object reference not set to an instance of an object

Comment
Add comment · Show 6
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 NoDumbQuestion · Oct 09, 2018 at 03:02 AM 0
Share

WallInformationSmall[i, j] is not a list and it is null and cannot add new object

avatar image Lunoxx · Oct 09, 2018 at 09:08 AM 0
Share

@NoDumbQuestion

Is this because it is not possible to make an array of lists how I tried at the top, or what am I doing wrong that it is not working as I for my understanding would expect? The thing is when I work with it, it always behaves like I wanted, there is no red underline or something, just when I want to run it it has the error.

avatar image NoDumbQuestion Lunoxx · Oct 09, 2018 at 09:21 AM 0
Share

ah my mistake. WallInformationSmall[i, j] is a list but it is just null.

In awake you did the loop wrong.

https://stackoverflow.com/questions/8184306/iterate-through-2-dimensional-array-c-sharp

Fix

 for(int i = 0; i < width / 10; i++)
           {
               for (int j = 0; j < height / 10; j++)
               {
                   WallInformationSmall[i, j] = new List<WallInformation>();
               }
           }

avatar image Lunoxx NoDumbQuestion · Oct 09, 2018 at 09:42 AM 0
Share

Sorry, my bad, the loop there was just a mistake I did on my own. I have it fixed a bit ago.

avatar image NoDumbQuestion Lunoxx · Oct 09, 2018 at 09:23 AM 0
Share

Whatever you are trying pull off with this code is just bad practice in general. Take my advice and reproach your problem and come up with better solution

avatar image Lunoxx NoDumbQuestion · Oct 09, 2018 at 09:46 AM 0
Share

I tried to get a bit into lists and found this was one of the best solutions for my problem, but made it a bit more complicated for myself that I wanted. But thank you, I think I will try to understand it a bit more and approach it differently. Also I have changed up something and it seems to be better now. I think it's not a nice way to work like that because you get confused yourself with that much indices and to always keep them in $$anonymous$$d.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lunoxx · Oct 10, 2018 at 02:57 PM

I think i got some kind of an answer, to my question, or at least i got the solution to the problem i was thinking of.

the problem was to make an array of lists, and how it is working, or add items to it.

and the solution was pretty easy.

 List<int>[] myList;
 
 void Start()
 {
 myList = new List<int>[sizeOfArrayOfLists];
 
 for (int i = 0; int < sizeOfArrayOfLists; i++)
 {
 myList[i] = new List<int>();
 }
 }


just wanted to share, if someone has the same problem. if i come across something that does not work with this, I will edit here.

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

169 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 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 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 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 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 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

Compare each vector3 from an array to each vector3 in a list and add objects which do not exist into the list 0 Answers

I have trouble understanding arrays and enums. When and how? 0 Answers

2D Arrays Help. Why to use 2D arrays for 2 ints 2 Answers

All GameObjects list to a GameObject? 0 Answers

Roughly how much processing power is needed to loop through string-Lists/Arrays when loading/saving stored data? 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