Search Unity

Unreacheble code detected

Discussion in 'Scripting' started by Confused_Jonas, Jan 12, 2021.

  1. Confused_Jonas

    Confused_Jonas

    Joined:
    May 28, 2020
    Posts:
    47
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.     public GameObject inventory;
    8.     private bool inventoryEnabled;
    9.  
    10.     private int allSlots;
    11.     private int enableSlots;
    12.     private GameObject[] slot;
    13.  
    14.     public GameObject InventorySlots;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         allSlots = 30;
    20.         slot = new GameObject[allSlots];
    21.  
    22.         for (int i = 0; i < allSlots; i++)
    23.         {
    24.             slot[i] = InventorySlots.transform.GetChild(i).gameObject;
    25.  
    26.             if(slot[i].GetComponent<Slot>().item == null)
    27.             {
    28.                 slot[i].GetComponent<Slot>().empty = true;
    29.             }
    30.         }
    31.     }
    32.  
    33.  
    34.     void Update()
    35.     {
    36.         if (Input.GetKeyDown(KeyCode.I))
    37.             inventoryEnabled = !inventoryEnabled;
    38.         if (inventoryEnabled == true)
    39.         {
    40.             inventory.SetActive(true);
    41.         }
    42.         else
    43.         {
    44.             inventory.SetActive(false);
    45.         }          
    46.     }
    47.     void ItemPickedUp(GameObject PickMeUp)
    48.     {
    49.         GameObject itemPickedUp = PickMeUp.gameObject;
    50.         Item item = itemPickedUp.GetComponent<Item>();
    51.  
    52.         AddItem(itemPickedUp, item.ID, item.type, item.description, item.icon);
    53.     }
    54.    
    55.     void AddItem(GameObject itemObject, int itemID, string itemType, string itemDescription, Sprite itemIcon)
    56.     {
    57.         for (int i = 0; i < allSlots; i++)
    58.         {
    59.             if (slot[i].GetComponent<Slot>().empty)
    60.             {
    61.                 itemObject.GetComponent<Item>().pickedUp = true;
    62.  
    63.                 slot[i].GetComponent<Slot>().item = itemObject;
    64.                 slot[i].GetComponent<Slot>().icon = itemIcon;
    65.                 slot[i].GetComponent<Slot>().type = itemType;
    66.                 slot[i].GetComponent<Slot>().ID = itemID;
    67.                 slot[i].GetComponent<Slot>().description = itemDescription;
    68.  
    69.                 itemObject.transform.parent = slot[i].transform;
    70.                 itemObject.SetActive(false);
    71.  
    72.                 slot[i].GetComponent<Slot>().UpdateSlot();
    73.                 slot[i].GetComponent<Slot>().empty = false;
    74.             }
    75.  
    76.             return;
    77.         }
    78.     }
    79. }
    what is the problem and how do i fix it? everything work fine, but i get that annoying message
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    It's just a warning, you can ignore it.

    What line does the warning say it's on?
     
  3. Confused_Jonas

    Confused_Jonas

    Joined:
    May 28, 2020
    Posts:
    47
    76. i know why there is a warning, but how do i remvoe it
     
  4. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    You are returning after the first iteration of your for loop.
     
  5. Confused_Jonas

    Confused_Jonas

    Joined:
    May 28, 2020
    Posts:
    47
    what else can i do?
     
  6. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Remove the return. Otherwise the code will only run for the first "slot".

    Code (CSharp):
    1.  void AddItem(GameObject itemObject, int itemID, string itemType, string itemDescription, Sprite itemIcon)
    2.     {
    3.         for (int i = 0; i < allSlots; i++)
    4.         {
    5.             if (slot[i].GetComponent<Slot>().empty)
    6.             {
    7.                 itemObject.GetComponent<Item>().pickedUp = true;
    8.                 slot[i].GetComponent<Slot>().item = itemObject;
    9.                 slot[i].GetComponent<Slot>().icon = itemIcon;
    10.                 slot[i].GetComponent<Slot>().type = itemType;
    11.                 slot[i].GetComponent<Slot>().ID = itemID;
    12.                 slot[i].GetComponent<Slot>().description = itemDescription;
    13.                 itemObject.transform.parent = slot[i].transform;
    14.                 itemObject.SetActive(false);
    15.                 slot[i].GetComponent<Slot>().UpdateSlot();
    16.                 slot[i].GetComponent<Slot>().empty = false;
    17.             }
    18.         }
    19.     }
     
  7. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Oh I see you are trying to add an item to the first empty slot. Sorry I didn't think it through.

    You want to place your return in the if() block instead:

    Code (CSharp):
    1.  void AddItem(GameObject itemObject, int itemID, string itemType, string itemDescription, Sprite itemIcon)
    2.     {
    3.         for (int i = 0; i < allSlots; i++)
    4.         {
    5.             if (slot[i].GetComponent<Slot>().empty)
    6.             {
    7.                 itemObject.GetComponent<Item>().pickedUp = true;
    8.                 slot[i].GetComponent<Slot>().item = itemObject;
    9.                 slot[i].GetComponent<Slot>().icon = itemIcon;
    10.                 slot[i].GetComponent<Slot>().type = itemType;
    11.                 slot[i].GetComponent<Slot>().ID = itemID;
    12.                 slot[i].GetComponent<Slot>().description = itemDescription;
    13.                 itemObject.transform.parent = slot[i].transform;
    14.                 itemObject.SetActive(false);
    15.                 slot[i].GetComponent<Slot>().UpdateSlot();
    16.                 slot[i].GetComponent<Slot>().empty = false;
    17.  
    18.             return;
    19.  
    20.             }
    21.          }
    22.     }
     
  8. Confused_Jonas

    Confused_Jonas

    Joined:
    May 28, 2020
    Posts:
    47
    thank you very much!
     
    flashframe likes this.
  9. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    For performance you should only use GetComponent once and cache it:

    Code (CSharp):
    1. for (int i = 0; i < allSlots; i++)
    2. {
    3.     Slot tempSlot = slot[i].GetComponent<Slot>();
    4.  
    5.     if (tempSlot.empty)
    6.     {
    7.         itemObject.GetComponent<Item>().pickedUp = true;
    8.         tempSlot.item = itemObject;
    9.         tempSlot.icon = itemIcon;
    10.         tempSlot.type = itemType;
    11.         tempSlot.ID = itemID;
    12.         tempSlot.description = itemDescription;
    13.         itemObject.transform.parent = slot[i].transform;
    14.         itemObject.SetActive(false);
    15.         tempSlot.UpdateSlot();
    16.         tempSlot.empty = false;
    17.  
    18.         return;
    19.  
    20.     }
    21. }
     
    NOT_A_ROBOT1101 likes this.