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*.isFull == false)*

{
//able to add item.

// set item in slot.
slots*.item = item;*
slots*.image.sprite = item.artWork;*
slots*.isFull = true;*
//disable adding item
break;
}

}

}

}

The code looks fine to me.

I’d guess slots is empty or slots*.isFull is already set to true on all items?*

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*.isFull == false && hasBeenAdded == false)*

{
//able to add item.

// set item in slot.
slots*.item = item;*
slots*.image.sprite = item.artWork;*
slots*.isFull = true;*
hasBeenAdded = true; //HERE
//disable adding item
break;
}

}
}

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

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