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 General_sTORe · Feb 01, 2017 at 07:44 PM · 2d gametransform.positionfor-looparray of gameobjectscomparison

For loop comparing two different objects appears to not be running

So I'm working on a match 3-esque game and I'm currently trying to get my objects to sense whether or not they are adjacent to another object of their color. And by adjacent I mean adjacent from every angle; diagonally, horizontally and vertically. I've currently stored the exact coordinates of each object in an array from a script that spawns all the objects to the level. The array's name is groupOfEnemies. On the object's script, I have created a method called MatchCheck() and I call it whenever I click on an object with OnMouseDown(). I've pasted the MatchCheck() code further below.

Essentially, due to the way I've set up my game's coordinates, I want each for loop to iterate down every possibly adjacent object, from left to right. To better explain, think of the numbers on a num pad as the locations of the objects in my next sentence: I want each object's color to be compared to the object at number 5, starting with the object at the number 7 and working my way down to 1. Then I want to start back on top at 8 down to 2, until I reach the final object at number 3. You'll notice that I set each for loops variable to check and iterate by 2. This has to do with how far apart my objects are from each other. So for example, since the object in the top left will, in relation to the center object, be -2 X and +2 Y coordinates away, I'm trying to tell the for loops to account for this mathematical consistency in relation to the current object doing the checking. The first if statement then makes sure the central object will not be checked for the same color, and then the second if statement checks if the object at the current coordinates has the same color as the central object (the body variable contains a string designating the object's color).

 public void MatchCheck()
     {
       int transX = (int)this.transform.position.x;
       int transY = (int)this.transform.position.y;
     
        for (int x = transX - 2; x > transX + 2; x += 2)
            {
               for(int y = transY + 2; y < transY - 2; y -= 2)
                  {
              if(enemyArray.groupOfEnemies[x,y].transform.position != this.transform.position)
                      {
               if(enemyArray.groupOfEnemies[x,y].GetComponent<BasicEnemy>().body == this.body)
                             {
                             match = true;
                             }
                         }
                     }
                 }
             print(match);
         }

My trouble is that it appears the for loop isn't running. I've tested that the method runs when called, but even when I put a Debug.Log after the first for loop, I get nothing. The only thing I can think of right now that might be causing the issue is my adding and subtracting from the for loop conditions, but I don't understand why. Is that's what causing the issue, and if so is there a better way around it? Or is there another problem here that I'm missing? If you think you need to see more code, please let me know.

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

Answer by jkramar · Feb 01, 2017 at 09:19 PM

it should read

  for (int x = transX - 2; x < transX + 2; x += 2)


your had a greater-than instead of a less-than

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 General_sTORe · Feb 01, 2017 at 09:59 PM 0
Share

Oh, yes, of course. I forgot the conditional statement waits until the result is false before stopping, rather than waiting until it's true. Thank you for clearing that up for me! :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't Locate gameobject in array 3 Answers

Moving in Y and X after collision 0 Answers

Need 2D help - Want to have object move towards point when it touches a trigger. 0 Answers

make a gameobject move to another gameobject that was selected by mouse click? 1 Answer

[Netcode/MLAPI] network object being visible across scenes 1 Answer


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