• 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 sidonk · Dec 24, 2015 at 03:05 AM · gameobjectgamelist

Help with checking if gameobjects are the same on diagonal

Hallo, so I'm working on this game, and i got a little problem here. I'm trying to check if objects are the same on the diagonal. The objects im checking on are stored in a list for every row, Im using the llst and checking if the obejcts in the colums are having the same tag, but they are not getting removed and the above are not getting pushed down in the list.

Sample Here are some of the code i use, I do this code in a lot of if else statements for all the cordinations and colors.

 if (row1.Units[0].CompareTag("blue") && row2.selectedUnits[1].CompareTag("blue") && row3.selectedUnits[2].CompareTag("blue"))
         {
 
             GameObject t1 = row1.Units[0];
             GameObject t2 = row2.Units[1];
             GameObject t3 = row3.Units[2];
 
             _GameManager.instance.PlayPoint();
 
             row1.Units.Remove(t1);
             row2.Units.Remove(t2);
             row3.Units.Remove(t3);
 
             Destroy(t1.gameObject);
             Destroy(t2.gameObject);
             Destroy(t3.gameObject);
 
             row1.Cblue -= 1;
             row1.tCount -= 1;
             row2.Cblue -= 1;
             row2.tCount -= 1;
             row3.Cblue -= 1;
             row3.tCount -= 1;
 
             _GameManager.instance.Points += 30;
             _GameManager.instance.needPoints -= 30;
             _GameManager.instance.Klax += 1;
             _GameManager.instance.needKlaxs -= 1;
 
             if (row1.Units[0] == null)
             {
                 for (int i = 0; i < row1.Units.Count - 1; i++)
                 {
                     if (row1.Units[i] != null)
                     {
                         row1.Units[i] = row1.Units[i + 1];
                     }
                 }
                 row1.Units[row1.Units.Count - 1] = null;
             }
 
             else if (row2.Units[1] == null)
             {
                 for (int i = 0; i < row2.Units.Count - 1; i++)
                 {
                     if (row2.Units[i] != null)
                     {
                         row2.Units[i] = row2.Units[i + 1];
                     }
                 }
                 row2.Units[row2.Units.Count - 1] = null;
             }
 
             else if (row3.Units[2] == null)
             {
                 for (int i = 0; i < row3.Units.Count - 1; i++)
                 {
                     if (row3.Units[i] != null)
                     {
                         row3.Units[i] = row3.Units[i + 1];
                     }
                 }
                 row3.Units[row3.Units.Count - 1] = null;
             }
 
         }

sometimes when I have an object getting added to one of the list it does what i want to, but it not consistently. And its like its never running the code even though i do it in update?

someone has any idéa whats could be wrong? (sorry for the english)

unavngivet.png (5.6 kB)
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
0
Best Answer

Answer by NoseKills · Dec 25, 2015 at 03:26 AM

This is the code for moving blocks down ?

 if (row1.Units[0] == null)
 {
        for (int i = 0; i < row1.Units.Count - 1; i++)
        {
              if (row1.Units[i] != null)
              {
                 row1.Units[i] = row1.Units[i + 1];
              }
        }
        row1.Units[row1.Units.Count - 1] = null;
 }

Looks wrong to me.

  1. if bottom square of column 1 is null (should be) ->

  2. start looping ->

  3. first loop : i == 0 if (row1.Units[0] != null) is false (bottom square should be empty/null) ->

  4. second loop : i == 1 if (row1.Units[1] != null) is true (second square is not empty) ->

  5. row1.Units[1] = row1.Units[1 + 1]; you put the top most square into the middle square.

The "if" inside your loops only overwrites existing blocks with blocks above them.

 if (row3.Units[i] != null) // if this is not an empty space
 {
        row3.Units[i] = row3.Units[i + 1]; // put the above block into it
 }


You just need a loop that finds every empty space and puts the block above it into it something like

 for (int i = 0; i < row1.Units.Count - 1; i++)
 {
     if (row1.Units[i] == null)
     {
          row1.Units[i] = row1.Units[i + 1];
     }
 }
 row1.Units[row1.Units.Count - 1] = null;

There's no need for the outer "if" because i think you always want to move blocks down if there's an empty space below it... this rule applies also if you create some mechanic to remove other blocks than just the 3 mentioned in your example.

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 sidonk · Dec 26, 2015 at 10:58 PM 0
Share

Thanks mate, this works sometimes, but its like my else if for the diagonal check is not working, i tried Debug.Log in the else if and its not firering when the conditions are right. Any idéa?

(in the upper first loop)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

43 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

Related Questions

"Field " " is never assigned to, and will always have it's default value null" 2 Answers

Get a selected item from a GameObject list? 0 Answers

Unity5/C# beginner here! Need some scripting help :) <3 1 Answer

What range of values need to be inserted if we need to pull a random item from the list? 0 Answers

How Do I Randomize Infinite Platform (With Respect To This Code)? 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