I'm working on a game here, and found this rather intriguing bug. Suppose you have this enum:
public enum ItemType
{
Food,
Weapon,
Tools,
Written,
Misc
};
a base class
public class BaseItem
{
public string Name = "Default Name";
public ItemType Type = ItemType.Misc;
}
and two instances of it:
Ins1 = new BaseItem
{
Name = "Something",
Type = ItemType.Food
};
Ins2 = new BaseItem
{
Name = "Something too",
Type = ItemType.Tools
}
This is what happened to me: The first instance's type would remain as pre-initialized in the base class, even though I specified in its constructor that I want the type to be Food. The 2nd instance's type would be set correctly to Tools.
IF, I added a new enum value BEFORE Food, such as:
public enum ItemType
{
Nothing,
Food,
Weapon,
Tools,
Written,
Misc
};
Then the 1st instance's type would be, as expected, Food. The 2nd instance's type would also be correct.
What could have caused this behavior? To describe it in short, all instances whose Type had been set in the constructor to the first value of the enum, would actually go back to the value they had in the BaseItem definition. Adding an extra value before the first enum value solved the problem, apparently; but it IS wrong, so I'd like to know what could have caused the issue.
Thanks!
--- Later Edit --- In case this helps: not doing any initialization to the "Type" field inside of BaseItem, and leaving only the braces constructor do the initialization, everything works fine without adding the "Nothing" value to the enum.
Sorry about this; after some more digging, it seems it's an Unity-only bug. Some other people encountered it too. I have solved the problem; everyone gets a vote up from me, and I'll add my own answer; maybe some other Unity users will find it. Thanks a lot for your help and interest!
publicfor non-abstract classes), so that's not a problem.