Game Development Reference
In-Depth Information
Let's remove some items
We can now add items to the inventory, so let's see whether we can remove them
too. Add this bit of code under our previous test code:
if(Input.GetButtonUp("Fire2"))
{
GameObject test2 = new GameObject();
test2.name = "Sword";
RemoveFromInventory(2, test2);
}
When we click the right mouse button, we again create a new GameObject. We also
set its name to "Sword" and then call the RemoveFromInventory function. In
the function call, we pass the 2 and the test2 GameObject. Now, when we run our
game and click the right mouse button, it will remove two of the sword objects from
our inventory. If "Sword" has a value of zero or less, the inventory slot will be set to
our Empty object.
To make sure this works, run the scene and left click your mouse a few times to
add a bunch of swords to the inventory. Now right-click a few times to remove some
swords. You can even remove them until you don't have any swords left and the slot
will be empty.
Search WWH ::




Custom Search