Custom Class variable changes at access of 1 instance

I know a similar question has been asked, right here, but i dont have a static variable in my class. My class is such:

class InventoryItem
	public var Name : String;
	public var Type : String;
	public var Info : String;
	public var Price : int;
	public var StackCap : int;
	public var Rarity : String;
	public var Amount:int;
	public var worldObject : GameObject;
	public var texRepresentation : Texture2D;
	public var texSaved:Texture2D;
	public var Selected:boolean=false;
	public var HoveringItem:boolean=false;

but later when i try to add items to my inventory, and change the Amount variable, it changes on any other one i’m working with. Is there a way to fix this other then typing out, NewItem.Name=OldItem.Name for every single variable?

the error occures around a line similar to this.

var NewItem=new InventoryItem();
item.Amount=RepeatAmount; // where repeat amount is = to leftover amount

so changing the item.Amount changes the NewItem.Amount too, and thats my issue.

When you do this:

var NewItem=new InventoryItem();

You’re assigning item to NewItem as a reference. It’s not a separate, unique variable. So anything that happens to item will also happen to NewItem, and vice versa. Probably the best thing to do is to make a method that copies the information from one InventoryItem instance to another. If you’re not familiar with value vs. reference types, this should help.

I was just looking into this as well. If you use:

class InventoryItem extends System.ValueType

Then your class can be copied around (instead of referenced around) using:


I think the class becomes a struct at this point. I found this page talking about creating structs in unityscript. And this page talking about value types vs reference types.