To further add to the great explanations above, as already mentioned when you declare an enum you actually declare a type. In the case of the OP the new enum type is a nested type since it was declared inside the Player class. However you can declare this type outside the Player class as well, which you would usually do when you want to use this type from more than one class. Of course since your enum type is public, you can use even the nested type from everywhere, but you have to refer to Player.WeaponType
when you want to use it from outside the Player class.
So this is also possible:
public enum WeaponType
{
Gun,
Knife,
Axe
}
public class Player : MonoBehaviour
{
public WeaponType currentWeaponType;
Next thing is as it has been mentioned, an enum is essentially just an seperate strongly typed version of another integer type. By default, if not stated otherwise, it’s the type “int”. Each of the enum’s member is just a named constant. By default consecutive members just get the next numerical value based on the value of their predecessor.
Imagine this enum:
public enum MyEnum : byte
{
A = 5,
B,
C,
D = 2
}
In this case MyEnum is not an Int32 but just a byte value. A obviously has a value of 5. B has a value of 6 and C has a value of 7 while D has a value of 2. They are really just named constants.
As mentioned an enum type is essentially just a strongly typed alias for the underlying type, in this case a byte. A variable of our enum type can take any byte value, even those who do not have a named constant. So we can do this:
MyEnum enumVar;
enumVar = (MyEnum)42;
This code does work and the enumVar will actually contain the value 42. When you assign one of the constants, the variable does not hold the name of that constant but just the value of that named constant.
MyEnum enumVar;
enumVar = MyEnum.A;
This stores the number 5 in our variable. Of course enums are usually meant to specifically restrict the numbers one can assign and to give those otherwise magic numbers a meaningful name. Since an enum is a new seperate type, it also avoids accidental misuse as you would need an explicit cast if you want to assign something that has a different type. So technically a variable of an enum type is just a variable of type int, byte, long, … (whatever the underlying type of the enum is). However it acts as a seperate type on the high level language side to avoid accidental misuse.