- Home /
Custom Inventory system: void argument not assigned properly?
I'm working on an inventory system and this script is for the equip part of that system. I have five slots to equip items and they work if I equip stuff manually. But now I am trying to write a void to equip them a bit easier. To get there I have this setup:
void Update () {
if (Input.GetButtonDown("Up")) {
equipItem (0, ItemType.Cap, cap);
print (cap.itemID);
}
}
and the void it self:
public void equipItem (int id, ItemType type, Item slot) {
for (int i = 0; i < database.items.Count; i++) {
if (database.items[i].itemID == id && database.items[i].itemType == type) {
slot = database.items[i];
print (cap.itemID);
print (slot.itemID);
}
}
}
Print (cap.itemID) returns as -1 (an empty Item) slot.itemID return as 0 (the ID that it is supposed to be). Did I make a mistake assigning the void arguments or is what I am trying to just dumb, and not possible/not the way to do it?
UPDATE: So the problem is that the Item "slot" get's changed but not the Item "cap" that should have been there in place of the Item "slot". I got it working by changing the whole void in to an new item, this way i could say cap = equipItem ( 0, itemTyp.Cap); and get what I want for now, problem is I also want to be able to acces this from other scripts.
UPDATE2: Never mind I can just find this script by tagging the object it is attached to.
Answer by FlaSh-G · Aug 25, 2014 at 05:01 PM
Paramters are local variables. Whatever you do to the variable slot within equipItem will not matter outside of it. You can return a value and assign it to "cap" doing this:
cap = equipItem (0, ItemType.Cap);
and
public Item equipItem (int id, ItemType type)
{
Item slot = null;
for (int i = 0; i < database.items.Count; i++)
{
if (database.items[i].itemID == id && database.items[i].itemType == type)
{
slot = database.items[i];
print (cap.itemID);
print (slot.itemID);
}
}
return slot;
}
Yeah, I figured that out myself as well (it is in the update). Thanks anyway!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Play method only ONCE when detected by the update function 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Weird Error c# 1 Answer