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 /
avatar image
0
Question by me2000 · May 09, 2016 at 07:23 PM · c#beginnerloopfor-looplooping

For loop not working as intended?!? (Beginner)

So I wrote a simple for loop in the update to check if my inventory system is full but it's not working, what am I doing wrong?

 for (int i = 0; i < Slots.Length; i++)
 {
     if(Slots[i].childCount == 1)
     {
         isfull = true;
     }
     else if(Slots[i].childCount == 0)
     {
         isfull = false;
     }
 }

isfull changes to true if all of my slots have a child as it should but if I then destroy a child, it won't make isfull false because the first if statement in this loop is somehow still changing isfull to true, even if that statement is not even applying anymore. If I just add break; in each if statement, the isfull bool will go crazy and be true from start no matter what the condition is.

Help please :/

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
1

Answer by pankajb · May 09, 2016 at 08:20 PM

Check using something like this...

 void CheckIfInventoryIsFull() {
 int totalInventoryInBag=0;
 for (int i = 0; i < Slots.Length; i++)
  {
      if(Slots[i].childCount == 1)
      {
            totalInventoryInBag ++;
      }
  }
 
 if(totalInventoryInBag == Slots.Length)
      {
           isfull = true;
      }
 else
      {
          isfull = false;
      }
 }
Comment
Add comment · Show 5 · 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 Bunny83 · May 09, 2016 at 11:54 PM 1
Share

Ins$$anonymous$$d of counting manually all slots that are filled and checking against the overall size, you can simply check if at least one slot is empty:

 isfull = true;
 for (int i = 0; i < Slots.Length; i++)
 {
     if(Slots[i].childCount == 0)
     {
         isfull = false;
         break; // stop the loop since the inv can't be full if one slot is empty
     }
 }
avatar image me2000 Bunny83 · May 10, 2016 at 10:32 AM 0
Share

I'm not entirely sure why isfull = true; is outside of any if condition or loop but this code works perfectly fine, thank you very much!

avatar image Bonfire-Boy me2000 · May 10, 2016 at 12:55 PM 0
Share

In your code, you're setting the flag every time you go round the loop. That means that isFull ends up as whatever the last time through sets it to (regardless of whether it found an empty one earlier in the loop).

In Bunny's code it's being set to true first and then negated only if it finds one that's empty. As soon as it does find an empty one, it doesn't need to look any further so it breaks out of the loop. If it gets all the way through the loop without breaking out, then it hasn't found any empty ones and isFull is still true. That's what you're after.

avatar image me2000 · May 10, 2016 at 10:28 AM 0
Share

There is a problem with your code, it keeps adding 100s-1000s to totalInventoryInBag with no limit. The for loop is in the update void.

avatar image pankajb me2000 · May 10, 2016 at 01:13 PM 0
Share

void CheckIfInventoryIsFull() { } is complete method... you should only call it when you want to check isFull status. Its not a good practice to put for loop in Update method.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Callback from a Loop 1 Answer

How to deactivate all GameObject in a array, except last one 4 Answers

C# List looping Vector3's 2 Answers

Loop Coroutine For A Demo Mode 1 Answer

How can use C# to make a loop run at maximum speed without stalling game time? 3 Answers


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