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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by kalovito66 · Dec 23, 2014 at 06:53 PM · move2d array

How to move cells in a 2d array

Hi people,

I am trying to move cells in the opposite direction in a 2d array for a match 3 game. Below is the script i am using

 private void DoEmptyDown(ref GameObject[,] cells)
     {   //replace the empty tiles with the ones above
         for (int x= 0; x <= cells.GetUpperBound(0); x++)
         {
             for (int y = 0; y <= cells.GetUpperBound(1); y++)
             {
                 var thisCell = cells[x, y];
                 if (thisCell.name == "Empty(Clone)")
                 {
                     for (int y2 = y; y2 <= cells.GetUpperBound(1); y2++)
                     {
                         if (cells[x, y2].name != "Empty(Clone)")
                         {
                             cells[x, y] = cells[x, y2];
                             cells[x, y2] = thisCell;
                             print ("done");
                             break;
                            }
                     }                
                 }
             }
         }
 }


As it is the script works fine, and shifts the gameobjects and fills the empty cells downwards. How can i shift the cells upwards instead of downwards. Below is what I have been trying to no luck to far. Only the immediate row moves up.

 private void DoEmptyDown(ref GameObject[,] cells)
     {   //replace the empty tiles with the ones above
         for (int x= 0; x <= cells.GetUpperBound(0); x++)
         {
             for (int y = 0; y <= cells.GetUpperBound(1); y++)
             {
                 var thisCell = cells[x, y];
                 if (thisCell.name == "Empty(Clone)")
                 {
                     for (int y2 = y; y2 >= cells.GetLowerBound(1); y2--)
                     {
                         if (cells[x, y2].name != "Empty(Clone)")
                         {
                             cells[x, y] = cells[x, y2];
                             cells[x, y2] = thisCell;
                             print ("done");
                             break;
                            }
                     }                
                 }
             }
         }
 }

Kindly assist. Thanks

Comment
Add comment · Show 3
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 Owen-Reynolds · Dec 23, 2014 at 07:21 PM 0
Share

There's nothing Unity-special about this. $$anonymous$$aybe someone wants to rewrite an old answer here. But I'd guess "shift 2D array" would pop up useful stuff.

Also, drawing a picture, with 0-n labels, often helps.

avatar image Victory Dan Greene · Dec 23, 2014 at 07:22 PM 0
Share

I'm not 100% clear what you're after, but have you tried making the for loop on line 5 count y down from UpperBound ins$$anonymous$$d of counting up from zero?

avatar image NoseKills · Dec 29, 2014 at 08:41 PM 0
Share

To echo what the guys here are saying, the middle for-loop at line 5 should probably count backwards in the second example since you want to start checking empty spaces from the direction the pieces would be falling to.

And since there's nothing magical happening here, drawing the grid on paper and following through the code loops while looking at the image should give you an understanding what's happening.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make your player not move during a animation? 0 Answers

move character controller forward using gui button. 1 Answer

objects to move when picked up 0 Answers

c# not going to destination on 0 hp 1 Answer

Monster doesn't move 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