Unreachable code detected in for loop, can't understand what's the problem

I’m making an inventory system, and in the part where picked up item must be added to the proper slot, I’m using for loop to check if the slot empty or not, and if it is - add an item. But when I’m doing return after if statement, i++ inside of for circle brackets becomes gray in Rider, and unity says “Assets\Scripts\PickupScript.cs(157,39): warning CS0162: Unreachable code detected”. If I make it without return, it’s going to fill all slots with that one item. Anybody help pls!

It’s function where that problem appears:

void AddItem(GameObject item,int itemID, string itemName, string itemDescription)
    {
        for (int i = 0; i < allSlots; i++)
        {
            if (slot[i].GetComponent<Slot>().empty)
            {
                //add an item to the slot
                slot[i].GetComponent<Slot>().item = item;
               
                slot[i].GetComponent<Slot>().ID = itemID;
                slot[i].GetComponent<Slot>().itemName = itemName;
                slot[i].GetComponent<Slot>().description = itemDescription;

                slot[i].GetComponent<Slot>().UpdateSlot();
                slot[i].GetComponent<Slot>().empty = false;
            }
            return;
        }
       
    }

Update slot function, to show that it’s void:

public void UpdateSlot()
    {
        gameObject.SetActive(true);
        text.text = itemName;
    }

And just in case AddItem function in context:

if (isInFront && Input.GetKeyDown(KeyCode.T))
        {
            Item itemInformation = item.GetComponent<Item>();
           
            AddItem(item, itemInformation.ID, itemInformation.itemName, itemInformation. description);
            itemPickedUp = true;
           
            isInFront = false;
            moveToPlayer = false;
            item = null;
        }

I don’t understand the point of the return… Can you explain why you need it? With the return, the AddItem method will always return after the first iteration of the loop, no matter what. It will never be able to operate on more than one element of the slot array or list. That’s why it’s saying there’s unreachable code.

1 Like

Ok I think I get what you’re going for. You want to add an item to the first empty slot in your inventory, right? What you want is to put the return inside the if statement. In other words, once you’ve found an empty slot and placed the item, then you return. The way you have it now outside of the if statement it will just return after looking at the first slot in the inventory, whether it was empty or not. It will not look at the rest of the inventory.

I did it following tutorial that tutorial he explains why it’s necessary on 13:48 :
https://www.youtube.com/watch?v=IhmqxXaK9hY

Also without return, for loop fills all inventory slots with that one object

Thank you, man! It works!

But one thing I can’t understand why it’s working in his case (also when I did it first time it’s been working too)

It will only work for the first slot that way (because the code only can look at the first slot before returning). It will not work for any slot past the first. The reason he did that in the tutorial is simply that he made a mistake in the tutorial.

Even the youtube comments have noticed this mistake:
6170000--675209--upload_2020-8-4_23-52-33.png

1 Like

Oh, get it now, my bad. Thanks a lot!)