- Home /
Can't access an enum. I get an error.
using UnityEngine;
using System.Collections;
public class BaseWeapon : BaseStatItem {
public enum WeaponTypes{
SWORD,
STAFF,
DAGGER,
BOW,
SHIELD,
POLEARM
}
private WeaponTypes weaponType;
}
The problem is when i try to make a private 'WeaponTypes'. Monodevelop tells me 'WeaponTypes' doesn't exist in the current context. What am I doing wrong?
I just copy and pasted your code above into $$anonymous$$onoDevelop and it built no issues!
The only thing I had to change was to remove the inheritance on the class (as I don't have your base class :) ).
What version of Unity/$$anonymous$$onoDevelop are you using? (I have 5.1.3 and 4.0.1 here).
Are you trying to access or have you declared WeaponTypes in the baseclass BaseStatItem perhaps?
No, I haven't declared it before then. I'm guessing it was just a bug...Thanks for the verification that it was.
Answer by Suddoha · Sep 09, 2015 at 06:49 PM
There is nothing wrong with the code provided and it should actually compile. Try to save everything, restart MonoDevelop.
In case you want to use WeaponTypes in another class though, you need to write BaseWeapon.WeaponTypes as it is defined in the BaseWeapon class. Note, that enums can also be defined outside of any class.
Yeah, I couldn't find anything wrong either. $$anonymous$$ust just be a bug. Thanks for the verification. Also, thanks for the tip. I did not know that before.
$$anonymous$$eep in $$anonymous$$d that you declared your enum as a nested type inside your BaseWeapon class. That means if you want to use the enum outside of the outer class (or outside of a derived class) you have to specify the class name as well:
public BaseWeapon.WeaponTypes someVariable;
or
someObject someVariable = BaseWeapon.WeaponTypes.SWORD;
If you want the enum to be a public accessible enum, move it's declaration out of the class (before or after the class declaration or into a seperate file.)
That's already in the answer, but thanks for adding an example ;)