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 kaiserkappel · Mar 05, 2015 at 02:41 AM · c#listrandombooleanmultidimensional array

List of List of Booleans function giving weird results

Hello there! I'm attempting to create a script that creates a 2D list of Boolean values. This 'grid' will be used for determining whether a certain position can hold an object or not. True for yes, false for no. The one stipulation is that the position cannot be true if the column above it is true. If the position above is false, we use an RNG to determine if it gets marked true or stays false. Here's an example:

[T, F, F, T, F]
[F, F, T, F, T]
[F, T, F, T, F]
... and so on

I wrote the following script to accomplish this:

     private int _gridSize;
     private List<List<bool>> _grid;
 
     void Awake()
     {
         //Set up size of grid and initialize the array
         _gridSize = 5;
         ResetGrid();
         //Pass a random first entry to kick things off
         GenerateGridPositions( new List<bool>() { true, false, true, true, false } );
     }
 
     void Update()
     {
         //Save last row of grid
         var lastRow = _grid[4];
         //Set all positions to false
         ResetGrid();
         //Generate based on saved row
         GenerateGridPositions(lastRow);
         //For Debug purposes
         for (int i = 0; i < _gridSize; i++)
         {
             Debug.Log(i + ":" + String.Join(",", _grid[i].Select(x=>x.ToString()).ToArray()));
         }
     }
 
     private void ResetGrid()
     {
         _grid = new List<List<bool>>();
         for (int i = 0; i < _gridSize; i++)
         {
             _grid.Add(new List<bool>());
         }
     }
 
     /// <summary>
     /// Sets the positions of the obstacles for the next street. Columns should not have obstacles in
     /// two successive rows. Probably should go into the obstacle manager.
     /// </summary>
     private void GenerateGridPositions(List<bool> lastRow)
     {
 
         //Set the first row based on the row passed
         for (int i = 0; i < _gridSize; i++)
         {
             if (!lastRow[i])
             {
                 _grid[0].Add(PositionChance());
             }
             else
             {
                 _grid[0].Add(false);
             }
         }

         //Set each subsequent row, based on the row positions above it
         for (int i = 1; i < _gridSize; i++)
         {
             for (int j = 0; j < _gridSize; j++)
             {
                 if (!_grid[i-1][j])
                 {
                     _grid[i].Add(PositionChance());
                 }
 
                 else
                 {
                     _grid[i].Add(false);
                 }
             }
         } 
     }
 
     /// <summary>
     /// Determines whether an obstacle is added to the grid. Potentially can be skewed to
     /// produce trues more often if there is a difficulty setting.
     /// </summary>
     private bool PositionChance()
     {
         var rand = Random.Range(0, 10);
         var choice = rand%2 == 0;
         return choice;
 
     }

But it does something weird upon attaching VS's debugger to the Unity process and setting breakpoints: I've found that when it attempts to add the next position to a row, it adds that position to all the rows. I'm not quite sure why it does this and I'm completely at a loss. I've also implemented this code in Python just to test it and it works exactly as intended. Does anyone see anything I'm not seeing?

Also of note: this function wouldn't normally be called every frame in Update(). I just have it in there for testing purposes.

Edit 2: Updated to reflect accepted answer.

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
1
Best Answer

Answer by Bunny83 · Mar 05, 2015 at 03:30 AM

Your problem ist this piece of code:

 _grid = new List<List<bool>>();
 _grid.AddRange(Enumerable.Repeat(new List<bool>(), _gridSize).ToList());

This will add the same inner List instance multiple times to your outer List. You only create one outer list and one inner list. So each "row" in your outer list references the same inner list.

You should initialize your grid like this:

 _grid = new List<List<bool>>();
 for(int i = 0; i < _gridSize; i++)
    _grid.Add(new List<bool>());
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 kaiserkappel · Mar 05, 2015 at 05:19 AM 0
Share

Perfect! Thank you. So if the first parameter of Repeat is a function or instantiation, then it runs that first and then repeats the reference over and over again? That kinda sucks. Is there a LINQ function that actually does what I was attempting?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to fix some of location have more than 1 object? 2 Answers

Making a camera list 1 Answer

my random task generator not working 1 Answer

A node in a childnode? 1 Answer

Randomizing numbers on a list. 4 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