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 question was closed Mar 07, 2014 at 02:51 PM by Dblfstr for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Dblfstr · Mar 06, 2014 at 11:19 PM · c#listif-statementsalgorithmlogic

Check if all values in a list are equal

alt textThis is probably simple but I have just stared at it too long. I have a 4X4 grid of gems. the grid is laid out on the x-z axis, but the height of the gems vary on the Y axis. I have a function that, when a gem is clicked, moves the gem down 1 unit. It also moves neighboring gems up. The point is to get all the gems on the same level (Think a 3-D version of lights out) My problem is I cannot get the logic right to check if each gems Y position is equal. I have an empty gameObject that holds a list of the gems. I am trying to search through it, and check that a gems has the same height as the last gem. If so, keep going. THen at the end, if all were equal. Say you won!. But sometimes I win even when they are not equal. Here is the code. Please ask if more info is needed.

 void Update () 
     {
         for(int i = 1;i<=gems.Count-1 ;i++)
         {
             //Debug.Log (gems.Count);
             Gem currentGem = gems[i-1];
             Gem nextGem = gems[i];
             if(currentGem.transform.localPosition.y == nextGem.transform.localPosition.y && hasWon)
             {
                 hasWon = true;
             }
             else{
                 hasWon = false;
             }
             if(i == gems.Count-1 && hasWon)
             {
                 Debug.Log("You Win!");
             }
             hasWon=true;
         }
 
 
     }


untitled-1.png (165.2 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

  • Sort: 
avatar image
1
Best Answer

Answer by SirCrazyNugget · Mar 06, 2014 at 11:33 PM

You should probably look at using a multidimensional array and storing them in the necessary XYZ addresses. That will allow you to check each type within the array rather than trying to depict where each one is.

 //if you continue with it the way you have it, just determine the Y position of the first Gem
 float posY = gems[0];
 
 //then presume they've won until you find a gem that isn't in the right position
 hasWon = true;
 for (int i = 1; i < gems.Count; i++){
     if(gems[i].transform.localPosition.y != posY){
         hasWon = false;
     }
 
 }
 
 //then determine after you've checked each one whether they've actually won and do something
 if(hasWon){
     //tada!
 }
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 Dblfstr · Mar 07, 2014 at 01:11 PM 0
Share

That's the ticket! And is what I thought I was trying to. Anyway, I only need to check the Y axis. The other x and z are irrelevant at this point. Also, the data is already contain in a list; one that is used because of its added functionality. I can alter the size, and add a remove components at will. There is also another list that stores what gem is next to each gem. This is how I will control the game logic when manipulating gems. ($$anonymous$$ove the gem up and the neighbors down, when a gem is clicked). Anyway, thanks for the proper game logic, I was struggling with this for a while. It happens when you stare at the same code for so long.

avatar image Dblfstr · Mar 07, 2014 at 01:54 PM 1
Share

I did have to modify one thing.

 float posY = gems[0].transform.localPosition.y

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Foreach with where with 2 lists 0 Answers

Brute Force Search Algorithm 0 Answers

False = true, what am I doing wrong? 2 Answers

Card/Tile Game, loops dont update element0 of playerHand properly 1 Answer

A node in a childnode? 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