Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

What is here the problem?

my items in my inventory system appear in a delay. after adding an item it appears when i add the next item :/ for example i have wood and a colt. when i add wood it dont appears. by adding the next item example the colt the wood appears. when i idd then agan ain colt or wood the colt appears.


Thats my Inventory script

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;


public class Inventory : MonoBehaviour

{


  public int slotAmount = 11;

  public GameObject slotPrefab;

  public GameObject slotParent;

  public GameObject InventoryObject;

  public GameObject itemPrefab;


  private MouseLook mouseLook;


  private ToolTip tooltip;


  public List<GameObject> slots = new List<GameObject>();

   


  private ItemDataBase itemDatabase;


  public KeyCode inventorykey = KeyCode.Tab;


  public bool isShown = false;

   


  // Start is called before the first frame update

  void Start()

  {


    CreateSlots(slotAmount);


    mouseLook = GameObject.FindObjectOfType<MouseLook>();


    itemDatabase = GameObject.FindGameObjectWithTag("ItemDataBase").GetComponent<ItemDataBase>();


    tooltip = GameObject.FindGameObjectWithTag("Tooltip").GetComponent<ToolTip>();


  }



  void Update()

  {

     

    if (Input.GetKeyDown(inventorykey))

    {

      isShown = !isShown;


      if(isShown == true)

      {

        Cursor.visible = true;

        tooltip.ToggleTooltip(false);

        Cursor.lockState = CursorLockMode.None;

        mouseLook.mouseSensitivity = 0f;


      }

      else if (isShown == false)

      {

         

        Cursor.lockState = CursorLockMode.Locked;

        mouseLook.mouseSensitivity = 900f;

        Cursor.visible = false;

      }


    }

    if(isShown == true)

    {

      InventoryObject.SetActive(true);


    }

    else if(isShown == false)

    {

      InventoryObject.SetActive(false);



    }

  }

  // Update is called once per frame

  void CreateSlots(int slotAmount)

  {

    for (int i = 0; i < slotAmount; i++)

    {

      slots.Add(Instantiate(slotPrefab));

      slots[i].transform.SetParent(slotParent.transform);

    }

  }


  public void AddItem(int id)

  {

    Item itemAdd = itemDatabase.GetItemByID(id);

    for (int i = 0; i < slots.Count; i++)

    {

      if(slots[i].transform.childCount <= 0)

      {

        GameObject itemInstance = Instantiate(itemPrefab);

        itemInstance.transform.SetParent(slots[i].transform);

        itemInstance.transform.localPosition = Vector2.zero;

        itemPrefab.GetComponent<Image>().sprite = itemAdd.itemSprite;

        break;

      }

    }     

  }

}


i think the reason is my "itemPrefab" because this is the first item which appears in my inventory but i dont know how i could fix it

"hoffentlich" is the sprite which is appearing first when i am picking something up

if you need my interact script or something like that, i will show it you

Best Answer

  • DragunasDragunas Member
    Accepted Answer

    ok guys i found the problem, after 8 hours of hard tryng and working i found the problem, i had to write itemInstance.GetComponent<Image> and not itemPrefab.GetComponent<Image>. I was searching a long time for this simple issue but at least i found it :D

Answers

Sign In or Register to comment.