Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MegaChuck64 · Sep 09, 2015 at 05:52 PM · monodevelopinheritanceenum

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LinkoVitch · Sep 09, 2015 at 06:43 PM 1
Share

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?

avatar image MegaChuck64 LinkoVitch · Sep 12, 2015 at 03:59 AM 0
Share

No, I haven't declared it before then. I'm guessing it was just a bug...Thanks for the verification that it was.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MegaChuck64 · Sep 12, 2015 at 03:56 AM 0
Share

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.

avatar image Bunny83 MegaChuck64 · Sep 12, 2015 at 11:01 AM 1
Share

$$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.)

avatar image Suddoha Bunny83 · Sep 12, 2015 at 11:37 AM 0
Share

That's already in the answer, but thanks for adding an example ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Grabbing an Inherited Variable 0 Answers

Autocomplete not working for inherted methods 1 Answer

Overriding an inherited Enum? 1 Answer

Problem accessing enum from other class 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges