- Home /
Image not display current slot location while add new item.


Hello, i am new to Unity and Unity's C#, and i tried to follow some tutorials on youtube. But some how i try to create a function to add new item, it seem work and in the slot panel display correct item"Hammer" but one thing is the image of that item is not display at current slot location. It Display out of the inventory panel.
In the void start(), when i add item. it display correctly.
public void AddItem(int id)
{
Item addNewItem = database.GetItemDetailFromDatabase(id);//retrieveItemFromItemDataBase
//check if the item is stackable
//check if the item is Exist in Inventory
if(addNewItem.Stackable && CheckIfItemIsInInventory(addNewItem))
{
for(int i = 0; i < items.Count; i++)
{
if(items[i].ID == id)
{
//get the item from the database
ItemData data = slots[i].transform.GetChild(0).GetComponent<ItemData>();
data.amount++;// add the item
data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();// show the amount in front end after add new amount.
break;
}
}
}
else
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].ID == -1)
{
items[i] = addNewItem;
GameObject itemObject = Instantiate(inventoryItem);
itemObject.GetComponent<ItemData>().item = addNewItem;
itemObject.GetComponent<ItemData>().amount = 1;
itemObject.GetComponent<ItemData>().slotLocation = i; //drag the item and if the slot is empty, it will updated the new slot that you put.
itemObject.GetComponent<Image>().sprite = addNewItem.Sprite;
itemObject.transform.SetParent(slots[i].transform);
itemObject.transform.position = Vector2.zero;
itemObject.name = addNewItem.Title;// display the item name
break;
}
}
}
}
item-display.png
(3.9 kB)
error.png
(33.1 kB)
Comment
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Camera main is not callable 1 Answer
random sprite javascript 0 Answers