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 Loki18000 · Dec 11, 2015 at 09:26 PM · c#arrayspriteloopfor-loop

if ALL items in array are something

Hi, I having some trouble finding out how to do a check to see if all instances in an array are equal to something not just the first one. I am having trouble finding the right question around the forums too. Everything that I try has the same outcome, just testing the first array item.

This is what I have

  for(int i=0; i < slots.Length; i++)
         {
             if (slots[i].sprite != null)
             {
                 nIndex = i;
             }
 }

So this I thought would check each item in the array to see when none of the items are null, so when all the items are set to a sprite. But it only checks the first item. That would be great if the inventory was only set to one carrying weight!

Thanks guys

Comment
Add comment · Show 1
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 BULK_BICEPS · Dec 11, 2015 at 11:56 PM 0
Share

Your loop looks fine. If your loop is only checking the first position, then there must be a problem with the way that you are creating the "slots" array. Try

 Debug.Log("Length of slots array: " + slots.Length);
 for(int i=0; i < slots.Length; i++)
          {
              if (slots[i].sprite != null)
              {
                  nIndex = i;
              }
  }

And see what it prints out.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by NoseKills · Dec 11, 2015 at 11:58 PM

But it only checks the first item.

Why do you think so? The only exit condition for your loop is i < slots.Length so you do check every item and set nIndex to be 'i' every time the sprite is not null. So after the looping, nIndex has the value of the last non-null item index in the array.

To answer your question, you have basically 2 options. Either check that all items fulfill your condition or check if even one item doesn't fulfill it. The latter way makes more sense because if you find even one item that fails the check, you don't even need to check the rest and you can break out of the loop.

 bool nullFound = false;
 for(int i=0; i < slots.Length; i++)
 {
              if (slots[i].sprite == null)
              {
                  nullFound = true;
                  break;
              }
  }

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 Loki18000 · Dec 12, 2015 at 02:44 PM 0
Share

Thank you, that worked great!

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

45 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 avatar image avatar image

Related Questions

Nested for loop isnt working 1 Answer

,Code in loop simultaneously executing on members of array rather than iterating one at a time 1 Answer

how to start counting from the last index the script stop on? 2 Answers

Can someone explain what IEnumerators and for statements are? 1 Answer

Same for each loop with multiple arrays? 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