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
1
Question by hherzog · Feb 20, 2013 at 01:41 PM · arrayif-statementsmatrix

As I compare 4 objects in an array?

I want to compare the objects around it: [X+1,Y][X-1,Y][X,Y-1][X,Y+1]

But the index like [X-1,Y]of the objetcs in the edges of the matrix is out of index.

 for (int i = 0; i <= array.Length; i++)
     {
         for (int j = 0; j <= array.Length; j++)
         {
              if    ((array[i, j].typeOfBlock!= array[i + 1, j].typeOfBlock) &&
                    (array[i, j].typeOfBlock != array[i - 1, j].typeOfBlock) &&
                    (array[i, j].typeOfBlock != array[i, j + 1].typeOfBlock) &&
                    (array[i, j].typeOfBlock != array[i, j - 1].typeOfBlock)
                  {
                      array[i, j].obj.renderer.material.mainTexture = 
                      ChooseTexture(array[i, j], 0);            
                  }
         }
      }

I tried to put if(i-1 >0) but keep throwing me an error.

I did things such as:

"if ((i + 1 <= 7) && (i - 1 >= 0) && (j + 1 <= 7) && (j - 1 >= 0))"

This last one works, but I let a lot of comparisons out this way. I did other things to try control the index but without success. how should I do this correctly?

I do not know why the code is not displaying properly, so click in edit to see it

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 Bunny83 · Feb 20, 2013 at 02:44 PM

The easiest way is to do 4 seperate checks

     for (int i = 0; i < array.GetLength(0); i++)
     {
         for (int j = 0; j < array.GetLength(1); j++)
         {
             var type = array[i, j].typeOfBlock;
             if (i+1 < array.GetLength(0) && type == array[i + 1, j].typeOfBlock)
                 continue;
             if (i-1 >= 0 && type == array[i - 1, j].typeOfBlock)
                 continue;
             if (j+1 < array.GetLength(1) && type == array[i, j + 1].typeOfBlock)
                 continue;
             if (j-1 >= 0 && type == array[i, j - 1].typeOfBlock)
                 continue;

             array[i, j].obj.renderer.material.mainTexture = 
             ChooseTexture(array[i, j], 0);            
         }
     }
Comment
Add comment · Show 6 · 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 hherzog · Feb 20, 2013 at 05:13 PM 0
Share

Thank you for the help, I didn't know about this continue keyword. But now throw an null error.

avatar image hherzog · Feb 20, 2013 at 07:40 PM 0
Share

is this the same thing that you have done?

                   var type = array[i, j].typeOfBlock;
                   if (i + 1 <= 7 && type != array[i + 1, j].typeOfBlock)
                   {
                       if (i - 1 >= 0 && type == array[i - 1, j].typeOfBlock)
                       {
                           if (j + 1 <= 7 && type == array[i, j + 1].typeOfBlock)
                           {
                               if (j - 1 >= 0 && type == array[i, j - 1].typeOfBlock)
                               {
                                   array[i, j].obj.renderer.material.mainTexture =
                                     ChooseTexture(array[i, j], 0);            
                               }
                           }
                       }
                   }
                  
avatar image Bunny83 · Feb 20, 2013 at 11:52 PM 1
Share

@hherzog: No, it's not the same. In your case no border piece can be precessed successfully because if one of your if statments "detects" and edge (so it can't check the actual type) it automatically fails. In my case i only check the type if there is a tile which can be checked. If there's no tile (because the index is out of bounds) this "side" is simply ignored.

So effectively if you just imagine the very first tile. It doesn't have one above (y-1) and doesn't have one on the left(x-1), but if the right tile(x+1) and the one below(y+1) matches it will execute your code.

Btw. I just copied the code in the first place, but you should keep in $$anonymous$$d that a multidimensional array has multiple length values, one for each dimension. You shouldn't use Length for them. There's a function GetLength(dimension) which will return the length of the given dimension.

I'll edit my answer...

avatar image Bunny83 · Feb 21, 2013 at 12:45 AM 1
Share

Just to clarify this. In my case i used "negative-logic". So my conditions test for cases when we don't want to execute the code.

You used positive-logic. So your conditions should evaluate to true when you want execute the code.

Your version should look like this:

 var type = array[i, j].typeOfBlock;
 if (i + 1 > 7 || type != array[i + 1, j].typeOfBlock)
 {
     if (i - 1 < 0 || type == array[i - 1, j].typeOfBlock)
     {
         if (j + 1 > 7 || type == array[i, j + 1].typeOfBlock)
         {
             if (j - 1 < 0 || type == array[i, j - 1].typeOfBlock)
             {
                 array[i, j].obj.renderer.material.mainTexture =
                 ChooseTexture(array[i, j], 0);
             }
         }
     }
 }

This might look a bit strange, but this is actually the same as my version. Basically each if statement is responsible for checking one side. If we can't check a certain side we want to assume that the check was successful. That's why either the index is out of bounds or the checked type is correct. If the index is out of bounds the "if" is already true (because of the "or") so the second condition, the actual check, isn't evaluated and that's what we want.

In general my version (which uses "early exits") is usually the one that can be understood best. The cascaded version is also better than one gigantic if statement but the disadvantage is that due to indention it grows to the right so it get's difficult to read / needs scrolling. It's also easy to get confused where a block begins and where it ends.

As one big "if":

 if ((i + 1 > 7 || type != array[i + 1, j].typeOfBlock)
  && (i - 1 < 0 || type == array[i - 1, j].typeOfBlock)
  && (j + 1 > 7 || type == array[i, j + 1].typeOfBlock)
  && (j - 1 < 0 || type == array[i, j - 1].typeOfBlock))
 {
     //[...]

ps I'll hope that "7" is your highest possible index and not the count. The count should be 8

avatar image hherzog · Feb 21, 2013 at 01:45 AM 0
Share

That was pretty enlightening,thank you very much.

I was thinking that I would need a lot of code just because with my code I couldn't handle it the border objects, and just a few "||" it would solve the problem

Show more comments
avatar image
1

Answer by flaviusxvii · Feb 20, 2013 at 02:17 PM

In the past, I've limited my loop to just the "inside elements" so they're free to compare against the "border elements". So my loops would look something like:

 for(int i = 1; i < arr.Length - 1; i++) {
     for(int j = 1; j < arr.Length - 1; j++) {
         // Here you can safely use arr[i-1][j-1].. things like that.
     }
 }
Comment
Add comment · Show 2 · 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 hherzog · Feb 20, 2013 at 02:54 PM 0
Share

is there a way to somehow do this with foreach?

avatar image Bunny83 · Feb 20, 2013 at 03:01 PM 1
Share

foreach? foreach doesn't work well with multi-dimensional arrays. Also you need to know the current i and j value or you can't compare them. See this SO question

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

11 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

Related Questions

how to create array with grid layout group 1 Answer

Rotate non square Color array by 90 degrees 1 Answer

Checking to see if a loaded level is equal to a member of an integer array 1 Answer

Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code. 2 Answers

Check if pressed key exists inside an array of KeyCodes 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