Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by morgan23 · Apr 20, 2014 at 04:05 PM · c#enumstatic script

Single enum class

My question is would it be best too make a static class that contains all my enums for my project or have them in a script that has too do with the enum.

Comment
Add comment
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

1 Reply

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

Answer by fendorio · Apr 20, 2014 at 04:16 PM

Just creating a .cs file will suffice - just as you'd create a class, it needs not be attached to any gameObject or inherit from MonoBehavior. So long as they're set to public you'll be able to access them.

 // Your .cs file

 public enum NumberOne
 {
      hello,
      this,  //Invalid syntax xD
      is,    //Invalid syntax xD
      accessible,
      anywhere
  }

 // End of .cs file

They need to be enclosed in a static class :)

As for whether or not you should store them all in one file, that's a subjective question really.

My opinion is no they shouldn't. Each Enum should be in it's own .cs file residing in an appropriate folder - say the folder that contains the class that uses it?

Again though purely subjective there is no right or wrong - apart from storing them in a static class :P

Comment
Add comment · Show 7 · 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 morgan23 · Apr 20, 2014 at 04:20 PM 0
Share

ah ok so public will work fine for just a script that holds all my enum's in my game ty. yeah I know what your saying I might use just a single from now on since it could be easier ins$$anonymous$$d of having in different classes.

avatar image Bunny83 · Apr 20, 2014 at 04:34 PM 1
Share

Enums don't need to be nested in a class at all. You can declare them outside a class as well. Enums are types like a class or a struct. They are often nested in the class they belong to, to keep the global namespace clean. Commonly you would declare them in your own namespace to avoid name-collissions with other types. For example the $$anonymous$$eyCode enum is declared inside the UnityEngine namespace, but outside any class.

avatar image TharosTheDragon Bunny83 · Oct 10, 2017 at 12:18 AM 0
Share

But is the $$anonymous$$eyCode enum declared in a file called $$anonymous$$eyCode.cs?

As far as I can tell, Unity wouldn't be able to find it otherwise.

avatar image Bunny83 TharosTheDragon · Oct 10, 2017 at 02:22 AM 0
Share

Not necessarily. We don't have the source code of the UntiyEngine.dll. We can only decompile the dll. Commonly every type defined in an assembly is decompiled into a seperate class with a matching filename. However you can have a file called "Test" which actually contains a class with the name Test and you can still declare the enum in the same file and it can be accessed in your whole project:

 // Test.cs
 using UnityEngine;
 
 public enum $$anonymous$$yEnum
 {
     One, Two, Three
 }
 
 public class Test : $$anonymous$$onoBehaviour
 {
     public $$anonymous$$yEnum fooBar;
 }

Unity's $$anonymous$$eyCode enum does only exist in compiled form to us. $$anonymous$$ost Unity engine types are defined in the UnityEngine.dll

Show more comments
avatar image morgan23 · Apr 20, 2014 at 04:47 PM 0
Share

I'm kinda confused by your comment bunny.

avatar image ghostmode · Oct 10, 2017 at 03:07 AM 0
Share

This is the correct answer, however I think you meant to say:

They don't need to be enclosed in a static class :)

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to assign Enums to button? 0 Answers

Accessing Enumeration from another script issue 1 Answer

how to get enum value by looking for their hashcode? 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