• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

problem with arrays and image replacment 0 Answers

Just another line renderer question. Line renderer vector positions in an array of game objects. My line only draws between 2 positions? 1 Answer

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

[C#] Array out of bounds 1 Answer

Same for each loop with multiple arrays? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges