- Home /
Inventory adds an item only once.
I have been following a youtube channel called Sykoo on how to make an inventory in unity and when I tested the code that I had written it seemed to work (although the game object item was added to both of my raw images) and the item was added to my inventory. After I exited the game and returned to it again it did not add to my inventory. Any help would be great thank you.Here is the code (if ive made a glaring mistake I may not have noticed it as Im a beginner to unity and sorry there is 3 scripts that make my inventory so there is a lot of code):
void Start()
{
inputDown = 0;
slots = slotHolder.transform.childCount;
slot = new Transform[slots];//Limit of slots //Slot is a new arra. Going to be amount of slots there are.
DetectInventorySlots();
}
public void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Item>())
{
itemPickedUp = other.gameObject;
AddItem(itemPickedUp);
}
}
public void OnTriggerExit(Collider other)
{
if(other.GetComponent<Item>()) {
itemAdded = false;
}
}
public void AddItem(GameObject item)
{
for (int i =0; i < slots; i++) //Check for each slot. this is a regular for loop that is created.
{
if (slot[i].GetComponent<Slot>().empty==false && itemAdded==false)//If the slot is empty
{
slot[i].GetComponent<Slot>().item = itemPickedUp;
slot[i].GetComponent<Slot>().itemIcon = itemPickedUp.GetComponent<Item>().icon;
itemAdded = true;
}
}
}
public void DetectInventorySlots()
{
for(int i = 0; i<slots; i++)//check for loop with integer called i at start. while i is lower then slots going to increase i by 1
{
slot[i] = slotHolder.transform.GetChild(i);//
print(slot[i].name);//print amount of slots
}
}
}
The second "Slot" script:
void Update()
{
if (item && itemIcon)
{
empty = false;//If there is an item empty is going to be false
itemIcon = item.GetComponent<Item>().icon; //the items icon is equal to the icon of that gameobject
this.GetComponent<RawImage>().texture = itemIcon; //The slots texture is equal to itemIcon (which is a texture)
}
else
{
empty=true;
}
}
}
The last script that is applied to the weapon (Item):
public string type; //Type of item
public int ID;
public string description;
public Texture icon;
public bool pickedUp;
Not many people want to wade through 200+ lines of code, try and trim it down to the lines you think to be the problem so we can help you quicker.
I hope I trimmed enough here. Sorry about that. If theres anything that needs to be added back in im more than happy to do that.
In leaving your game and reentering your game do you mean changing scene or pressing the play and stop button? If so, you need to save your inventory data.
When i press the play and stop button. As in I play it once then it works and adds to my inventory then I press stop then I press play again and it doesnt add to my inventory.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200135im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I detect if box is touching a tag named weapon? 1 Answer
PickUp Weapon in Brick Breaker Game 1 Answer
Player Animation change when pickup Weapon 0 Answers
Pick up gameobject and go to inventory, 1 Answer
Switching GameObject 1 Answer