Breaking "for" loop - still looping

Hi, here is my code

	void AddItem(int id, int Amount)
	{
		print("Searching inventory 

################");
for(int i = 0; i < inventory.Count; i++)
//Looping all slots
{
if(inventory*.itemName == null)*

  •  		//if empty slot*
    
  •  	{*
    
  •  		if(!InventoryContains(id))*
    
  •  		{*
    
  •  			print("I didn't find item with ID " + id + ", slot available 
    

################");*

  •  			for(int d = 0; d < database.items.Count; d++)*
    
  •  				//search for item in database*
    
  •  			{*
    
  •  				print("Searching for item with ID " + id + " in database 
    

################");*

  •  				if(database.items[d].itemID == id)*
    
  •  					//if ID from AddItem() = ID in database*
    
  •  				{*
    
  •  					print("I've found item with ID " + id + " in base, adding to inventory 
    

################");*
_ inventory = database.items[d];_
* print(“Set Count " + Amount + " for item with ID " + id + "
################”);*

_ inventory*.countNow += Amount;
break;
}
}
break;
}
else*
* {
print(“I’ve found item with ID " + id + " in slot " + i + "
################”);
break;
}
}
}
}
and everything is fine, but i don’t know why, script still looping database for item even if there is “break;”
All the time i have
> Searching for item with ID x in database*
x3 in log ;/
i think i fucked up something in this loop
for(int d = 0; d < database.items.Count; d++)_

In this case, you might give return a try instead of break. Alternatively you could add a label at the bottom of the function like this

void AddItem(etc...)
{

    //loops here

    BreakLoops:

}

Then you would use goto

goto BreakLooks;

to jump out of both for loops at the same time

No offense, your code was a little hard to read, so I ended up rewriting it:

// First of all, I'm assuming you've got a list of valid ID's floating around somewhere...
List<int> database;

// Additionally, I'm assuming your inventory is an array of items.
Item[] inventory;

// And, I'm assuming there's some definition of an Item that goes something like...
public class Item {
    public int itemID;
    public int amount;
}

// Return a boolean, stating whether we successfully added the item or not.
bool AddItem(int id, int amt) {
    // First, is this even a valid id?
    if(!database.Contains(id)) {
        // No?  Log message and return false - add failed.
        Debug.LogWarning("AddItem failed - invalid ID", this);
        return false;
    }
    
    // Next, is this a valid amount?  (aka more than none)
    if(amt < 1) {
        // No?  Log message and return false - add failed.
        Debug.LogWarning("AddItem failed - invalid amount", this);
        return false;
    }
    
    // Since we seem to have a valid input, now to actually add the item.
    
    // First, add to a stack that's already in this inventory.
    for(int i = 0; i < inventory.Count; i++) {
        // Is there an item here?
        if(inventory *!= null) {*

// Is this item’s ID the same?
if(inventory*.itemID == id) {*
// Cool! Add and return true!
inventory*.amount += amt;*
return true;
}
}
}
// If we get here, we couldn’t find a similar stack in the inventory.
// So, we make a new item in the first empty slot.
for(int i = 0; i < inventory.Count; i++) {
// Is there an item here?
if(inventory == null) {
// Cool! Make a new thing here…
inventory = new Item();
// …set ID…
inventory*.itemID = id;*
// …set amount…
inventory*.amount = amt;*
// …and return true.
return true;
}
}
// If we get here, we couldn’t even find an open spot in this player’s inventory.
// They’re too full up on other junk.
// Log a message and return false.
Debug.LogWarning(“AddItem failed - inventory full”, this);
return false;
}