2 Variables Acting like they are the same
So I am making an inventory system and am trying to make it so that half of the stack is picked up when right clicked. However when i copy the item in the slot to the floating item they act like they are the same variable so when I change the stack count to changes on both.
eg it I subtract 1 from the other the other also has 1 subtracted.
int giveNumber = (item.stackCount + 1) / 2;
parentInventory.floatingItem = item;
item.stackCount -= giveNumber;
parentInventory.floatingItem.stackCount = giveNumber;
if(item.stackCount < 1)
{
item = new Item();
}
could someone please help me as I don't know why the values are attached.
Answer by HenryStrattonFW · Feb 12, 2017 at 12:23 AM
Looking at your code, and assuming that item is a class type and not a struct, then this is because they are the same thing.
You set floatingItem = item that means that both variables, item and floatingItem are just references to the same underlying object.
But this is assuming like i said that item is of a class type and not a struct (value type).