Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Nov 10, 2021 at 01:12 PM by RayyFarr for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RayyFarr · Nov 10, 2021 at 05:02 AM · for-loop

for loop not breaking with if statement

i have a simple script for inventory and a script called slot. the slot script has a bool variable called isFull there are 10 slots in the scene. in the script below the for loop is supposed to loop through all slots until it find a slot with isFull false and set it to true. but for some reason it set isFull true for all slots.

any help would be appreciated.

thanks.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class Inventory : MonoBehaviour
 {
  
     public slot[] slots;
 
     public void addItem(Item item)
     {
         for (int i = 0; i < slots.Length; i++)
         {
 
             if(slots[i].isFull == false)
             {
                 //able to add item.
                 
                 // set item in slot.
                 slots[i].item = item;
                 slots[i].image.sprite = item.artWork;
                 slots[i].isFull = true;
                 //disable adding item               
                 break;
             }
 
         }
 
 
     }
 
 }
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 unity_ka6jgzfPPmtNCw · Nov 10, 2021 at 06:42 AM 0
Share

The code looks fine to me.

I'd guess slots is empty or slots[i].isFull is already set to true on all items?

avatar image RayyFarr unity_ka6jgzfPPmtNCw · Nov 10, 2021 at 11:04 AM 0
Share

i looked at it but thats not the case. isFull is false and slots array is full.

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by RayyFarr · Nov 10, 2021 at 11:26 AM

i fixed the issue. the problem was on another script. the issue was really stupid. here is the script its called pickup

 using UnityEngine;
 
 public class Pickup : MonoBehaviour
 {
     public Item item;
     public Inventory inventory;
 
     
 
     private void Start()
     {
 
     }
     void OnTriggerEnter (Collider other)
     { 
         if(other.CompareTag("Player"))
         {
             for (int i = 0; i < inventory.slots.Length; i++)
             {
                 if(!inventory.slots[i].isFull)
                 {
                     inventory.addItem(item);
                     Debug.Log("Adding item");
                     Destroy(gameObject);
                 }
             }
             
 
         }
     }
 
 }
 

after fix

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Pickup : MonoBehaviour
 {
     public Item item;
     public Inventory inventory;
 
     
 
     private void Start()
     {
 
     }
     void OnTriggerEnter(Collider other)
     {
         inventory.addItem(item);
         Debug.Log("Adding item");
         Destroy(gameObject);
     }
 }

fore some reason i didnt check this script.

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

Answer by evskii · Nov 10, 2021 at 08:39 AM

Try using return; rather than break; and see what happens.

return; exists the whole method whereas break; only exists the current switch/loop so using return; might help.

Otherwise you can add an extra check to see if an item has been added that method call. E.G:

 public void addItem(Item item)
      {
          bool hasBeenAdded = false; //HERE
          for (int i = 0; i < slots.Length; i++)
          {
  
              if(slots[i].isFull == false && hasBeenAdded == false)
              {
                  //able to add item.
                  
                  // set item in slot.
                  slots[i].item = item;
                  slots[i].image.sprite = item.artWork;
                  slots[i].isFull = true;
                  hasBeenAdded = true; //HERE
                  //disable adding item               
                  break;
              }
  
          } 
      }
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
avatar image
1

Answer by Bunny83 · Nov 10, 2021 at 09:12 AM

I'll assume that your slots array contains the same slot instance multiple times. So setting one would indeed affect all since there's only one slot. Using break or return would not make a difference and your code should work as it is, given each array element is actually a seperate slot instance (which I doubt given your problem).

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 Bunny83 · Nov 10, 2021 at 09:14 AM 0
Share

ps: class or generally type names should start with a capital letter.

Follow this Question

Answers Answers and Comments

133 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 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 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

Why can't i set my integer to -1? 2 Answers

adding "X" amount of objects to an array 2 Answers

Sorting a list into multiple smaller lists 1 Answer

Glitch in Monodevelop for loops!!! 1 Answer

Infinite loop with "for" that I'm really bad at finding. -1 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