Search Unity

Bug i get alot of errors

Discussion in 'Scripting' started by Khabi12, Sep 26, 2022.

  1. Khabi12

    Khabi12

    Joined:
    Aug 9, 2022
    Posts:
    22
    it does not work... anyway here is the code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InventorySystem : MonoBehaviour
    6. {
    7.  
    8.    public static InventorySystem Instance { get; set; }
    9.     public GameObject inventoryScreenUI;
    10.  
    11.     public bool isOpen;
    12.  
    13.     public List<GameObject> slotList = new List<GameObject>();
    14.  
    15.     public List<string> itemList = new List<string>();
    16.  
    17.     private GameObject itemToAdd;
    18.  
    19.     private GameObject whatSlotToEquip;
    20.  
    21.     //public bool isFull;
    22.     private void Awake()
    23.     {
    24.         if (Instance != null && Instance != this)
    25.         {
    26.             Destroy(gameObject);
    27.         }
    28.         else
    29.         {
    30.             Instance = this;
    31.         }
    32.     }
    33.  
    34.     void Start()
    35.     {
    36.      
    37.         isOpen = false;
    38.  
    39.         PopulateSlotList();
    40.  
    41.  
    42.        
    43.     }
    44.  
    45.         // it makes it so i dont need to drag and drop the slots to the slotlist [DOES WORK NOW]
    46.     private void PopulateSlotList()
    47.     {
    48.        foreach (Transform child in inventoryScreenUI.transform)
    49.        {
    50.  
    51.           if (child.CompareTag("Slot"))
    52.           {
    53.            Debug.Log("slotlist is working");
    54.              slotList.Add(child.gameObject);
    55.           }
    56.        }
    57.     }
    58. // makes it so i can open the inventory
    59.     void Update()
    60.     {
    61.         if (Input.GetKeyDown(KeyCode.E) && !isOpen)
    62.         {
    63.             Debug.Log("E is pressed");
    64.             Cursor.lockState = CursorLockMode.None;
    65.             inventoryScreenUI.SetActive(true);
    66.             isOpen = true;
    67.         }
    68.         else (Input.GetKeyDown(KeyCode.E) && isOpen);
    69.         {
    70.             Cursor.lockState = CursorLockMode.Locked;
    71.             inventoryScreenUI.SetActive(false);
    72.             isOpen = false;
    73.         }
    74.  
    75.  
    76.         void AddToInventory(string itemName)
    77.         {
    78.  
    79.                isFull = true;
    80.      
    81.                 Debug.Log("inventory is full");
    82.          whatSlotToEquip = FindNextEmptySlot();
    83.  
    84.          itemToAdd = Instantiate(Resources.Load<GameObject>(itemName), whatSlotToEquip.transform.position, whatSlotToEquip.transform.rotation);
    85.          itemToAdd.transform.SetParent(whatSlotToEquip.transform);
    86.  
    87.          itemList.Add(itemName);
    88.          
    89.          
    90.        
    91.          
    92.         }
    93.     }
    94.  
    95.      CheckIsFull();
    96.     {
    97.  
    98.         isFull =true;
    99.  
    100.        int counter = 0;
    101.  
    102.        foreach (GameObject slot in slotList)
    103.        {
    104.           if (slot.transform.childCount >0)
    105.           {
    106.              counter == 1;
    107.           }
    108.  
    109.        }
    110.        if (counter == 21);
    111.           {
    112.              return true;
    113.           }
    114.           else
    115.           {
    116.               return false;
    117.           }  
    118.     }
    119.  
    120.     FindNextEmptySlot();
    121.     {
    122.      
    123.        foreach(GameObject slot in slotList)
    124.        {
    125.          Debug.Log("2");
    126.           if (slot.transform.childCount == 0)
    127.           {
    128.             Debug.Log("working 2");
    129.              return slot;
    130.           }
    131.        }
    132.  
    133.        return new GameObject();
    134.     }
    135. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    You get a lot of errors but don't say what they are. You say it does not work but do not say what it's supposed to do when it does nor what doesn't work.

    Please put some effort into describing what it is you want devs to look at.

    Thanks.
     
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I can imagine some of the errors with that code, that won't compile. I'd say the start would be to do some base tutorials on C#.
     
  4. Khabi12

    Khabi12

    Joined:
    Aug 9, 2022
    Posts:
    22