Search Unity

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

Discussion in 'Scripting' started by Triton556, Aug 5, 2020.

  1. Triton556

    Triton556

    Joined:
    Jul 15, 2019
    Posts:
    5
    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:

    Code (CSharp):
    1. void AddItem(GameObject item,int itemID, string itemName, string itemDescription)
    2.     {
    3.         for (int i = 0; i < allSlots; i++)
    4.         {
    5.             if (slot[i].GetComponent<Slot>().empty)
    6.             {
    7.                 //add an item to the slot
    8.                 slot[i].GetComponent<Slot>().item = item;
    9.                
    10.                 slot[i].GetComponent<Slot>().ID = itemID;
    11.                 slot[i].GetComponent<Slot>().itemName = itemName;
    12.                 slot[i].GetComponent<Slot>().description = itemDescription;
    13.  
    14.                 slot[i].GetComponent<Slot>().UpdateSlot();
    15.                 slot[i].GetComponent<Slot>().empty = false;
    16.             }
    17.             return;
    18.         }
    19.        
    20.     }
    Update slot function, to show that it's void:

    Code (CSharp):
    1. public void UpdateSlot()
    2.     {
    3.         gameObject.SetActive(true);
    4.         text.text = itemName;
    5.     }
    And just in case AddItem function in context:

    Code (CSharp):
    1. if (isInFront && Input.GetKeyDown(KeyCode.T))
    2.         {
    3.             Item itemInformation = item.GetComponent<Item>();
    4.            
    5.             AddItem(item, itemInformation.ID, itemInformation.itemName, itemInformation. description);
    6.             itemPickedUp = true;
    7.            
    8.             isInFront = false;
    9.             moveToPlayer = false;
    10.             item = null;
    11.         }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    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.
     
    SerialArtistW likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    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.
     
  4. Triton556

    Triton556

    Joined:
    Jul 15, 2019
    Posts:
    5
    I did it following tutorial that tutorial he explains why it's necessary on 13:48 :


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

    Triton556

    Joined:
    Jul 15, 2019
    Posts:
    5
    Thank you, man! It works!
     
  6. Triton556

    Triton556

    Joined:
    Jul 15, 2019
    Posts:
    5
    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)
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    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:
    upload_2020-8-4_23-52-33.png
     
    Kurt-Dekker likes this.
  8. Triton556

    Triton556

    Joined:
    Jul 15, 2019
    Posts:
    5
    Oh, get it now, my bad. Thanks a lot!)