Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I make a public enum?

Discussion in 'Scripting' started by Marscaleb, Jan 20, 2015.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    I'm making a script, and I want it to have a public enum variable so I can set it in the inspector.

    But when I try this, I get an error saying the variable itself is more accessible than the variable's type. I assume this is because I declared the enum inside that one class, and so the enum is only accessible to that one class.

    How am I supposed to do it?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You've answered your own question here.

    Declare the enum outside of the class, at the namespace level.
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    Okay, what specifically is the namespace level?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    C# or JavaScript?

    In C# its the same level where you write class. As in

    Code (CSharp):
    1. public class MyComponent : MonoBehaviour {
    2.     // Do something awesome
    3. }
    4. public Enum MyEnum {valueA, valueB, ...}
    I have no idea how this is done in JavaScript, but there will be something similar available.
     
    nguyenhuuphu97 likes this.
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    But that's not what the error is about. It's about scope visibility.

    Default member scope is private, so unless the enum is declared as public it can't be seen from outside the class. The compiler is smart enough to realize that this means it can't be used as a part of the public interface for the class, because it means that things which can see the interface can't see the type required. Hence the message saying that the variable (the public thing which uses the enum) is "more accessible" than the type (the enum declaration itself).

    So... all you need to do is add 'public' before the enum type declaration.
     
    ozkntn and Kiwasi like this.
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    Okay, it's compiling now! Angrypenguin had what I was missing.
    Thank you!
     
    angrypenguin likes this.
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    personally, I ALWAYS define access modifer of anything I define.

    I never say:

    void Foo
    class Bar
    enum Grrr

    it's always

    private void Foo
    public class Bar
    internal enum Grr

    (modifiers are arbitrary for example)
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Probably good practice if you work across multiple languages. I get lazy and let the privates fall out to default.
     
  9. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    But what about the argument that states that you must not know the language. Because the language spec states that by default members are private and classes are internal. If you knew that then you would not need to always define access modifiers.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    First... I started the statement with "personally". So it's just my practice.

    Second... it's because I've worked on several large teams through out my career. Several of those projects spanned across multiple tiers, and would be written in different languages depending which part of the system it was in. Being explicit in what you write makes the code more readable, as opposed to relying on default behaviours, which requires considering more information that otherwise wouldn't have to be kept in mind.

    "Why the hell isn't the data contract for the new customer table on the database not being found when I import the data connection dll?"

    "Ummm... not sure... uh, everything looks fine. It's compiled in there. No errors. The unit tester is working fine...." what could be several minutes later, "aw, crap, Joe forgot to mark it public... stupid me, for not catching that sooner, and stupid joe for forgetting."

    When you work in teams like that, you develop standards that ease the working process. The language designers often sometimes come up with standards within their languages that don't actually translate well to real world application.


    And actually, that would be my argument TO write it explicitly all the time. Knowing the default access modifier behaviour is requiring you to know more about the language.
     
    Last edited: Jan 20, 2015
    angrypenguin and Kiwasi like this.
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    When I need to consider a practice within a project, I take the path of least immediate knowledge necessary.

    "When declaring a data type, I declare the access modifier"

    "When declaring a data type, it's access modifier is by default X if it's data type A,B, or C, and Y if it's D,E, or F. Unless I otherwise declare the access modifier explicitly."

    Which is the least amount of effort to remember? When you have 1000 of these little things to remember, which would you rather spend your limited brain resources on?

    As my father always said, "First you learn the right way, then you learn the short cut. That way when the short cut fails, because it will, you have the right way to fall back on." He may have only been a hillbilly farmboy truck driver... but the man was smart.