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 Darkgecko777 · Feb 19, 2016 at 01:43 PM · arrayif-statementsbooleans

How to return an index value from an array

How would I go about getting the index of an element at an array if it meets particular criteria. For example I have two arrays of 6 boolean elements which are performing similar functions. For the first only one element can ever be true and I would like to get the index value of the element which is true. For the second any number could be true and I would like to flag all which are true. So far creating a for loop and iterating through each has created some problems when I try to display the values. Either I don't get the values past the first element that is true or I can only get the last value.

Comment
Add comment · Show 2
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 Cherno · Feb 19, 2016 at 02:52 PM 0
Share

Post your loop code and we can see what might have gone wrong.

avatar image Darkgecko777 · Feb 19, 2016 at 07:26 PM 0
Share
 for (int y = 0; y < 6; y++){
         // int values
         burgherstxt[y].text = tile.burgher[y].ToString();
         peasantstxt[y].text = tile.peasants[y].ToString();
         warriorstxt[y].text = tile.warrior[y].ToString();
         uniquestxt[y].text = tile.unique[y].ToString();
 
         // boolean values 
             if (tile.influence[y]){
             influencetxt.text = "Player " + (y + 1);
             }  else {
             influencetxt.text = "None";
             } 
     }

I have 6 players, each of which can have pieces on a given tile. This loop works fine for the arrays of ints as I am just displaying their values corresponding to a text box. However for referencing the array of booleans (tile.influence[]) I want to return the index of the value which is true (only one can be true). If none are true then I want to display that as well. As I recall this particular loop will only check the last value and display that. It occurs to me that it is changing the text value 6 times as it iterates through each which would explain why I only get the last value to display. I tried adding in a return line for when it is true but that led to other problems. Ideally I would like to place this outside of my loop altogether, hence my question about flagging an index value where the element is true.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Darkgecko777 · Feb 19, 2016 at 10:11 PM

Sorted it out. While I was not able to determine the index of a given value as per my original question I did solve my issue. As well, the documentation for arrays and lists would indicate that there is some way to find an index but it looks like I can bypass that altogether. I put the following line after my loop rather than use the else in the loop.

 if (!tile.influence.Contains(true)){
         influencetxt.text = "None";
                 }

Now if all values return false I get the message I want.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

complete code in an if-statement even if statement turn false 2 Answers

Problem with IF-statement 2 Answers

Issue with if-statements requiring two conditions. 1 Answer

Set Active Random GameObject from array (JavaScript) 1 Answer

add button to switch cars 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