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
0
Question by darthbator · Jul 07, 2012 at 10:23 AM · getcomponentenum

(C#) How to access enum from a remote object?

Hey guys I was wondering how I might be able to access a remote enum in C#. Right now i have this enum setup in a kinda global "meta" object. I was planning on using this as a sort of index for my layers.

public enum Layers { playerActorLayer = 8, civilianActorLayer = 9, enemyActorLayer = 10, };

Now I try and grab this in the awake function of my other script as follows

 myLayers = GameObject.Find("debug_terrain").GetComponentInChildren<enumLayers>();

Which should grab the script component with the public enum from the child object it's living in on my terrain. However I can't seem to access myLayers.Layers in the other scirpt. Am I not handling this the right way? I have the enum scroped public. Is there something specific to enums that I am missing?

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

2 Replies

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

Answer by whydoidoit · Jul 07, 2012 at 10:24 AM

The enum is a global static thing scoped by the class it belongs to so it looks like you should be accessing it like this:

   enumLayers.Layers  
    
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 darthbator · Jul 07, 2012 at 11:36 PM 0
Share

Does this mean that I do not need to instantiate an object in order to access the enum? I can just call it from any other script in the scene kinda as follows?

case enumLayers.Layers.playerActorLayer :

???

Is an enum always considered to be static even if not implicitly cast as such?

avatar image whydoidoit · Jul 08, 2012 at 08:05 AM 0
Share

The definition of an Enum is always static yes - obviously you would need an instance to find a variable of that enum that was attached to a class.

So in your example in the comment you would find that the switch in which the case you write exists needs to access a variable, but the case statement will just use the definition as you write it.

avatar image darthbator · Jul 10, 2012 at 10:58 AM 0
Share

I don't entirely understand why I would need to get the enum in a variable outside of the "body" of the switch statement but am able to access it via it's definition inside the switch (for example in that case line there).

Thanks so much for all the help!

avatar image
0

Answer by nventimiglia · Jul 08, 2012 at 01:44 AM

All an Enum is a glorified int. I believe you fundamentally misunderstand it.

 public enum NumberEnum{
 One = 1,
 Two = 2,
 Joker= 3,
 }
 
 void Awake(){
 var one = 1;
 var two = 2;
 var three = 3;
 
 if(one == (int)NumerEnum.One)
  Debug.Log("One");
 if(two == (int)NumerEnum.Two)
  Debug.Log("Two");
 if(three== (int)NumerEnum.Joker)
  Debug.Log("Joker");
 }
Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

How do I call an enum from another script? 3 Answers

Accessing Enumeration from another script issue 1 Answer

error BCE0022: Cannot convert error, please help 3 Answers

Neater way to test an Enum in a different script? 1 Answer

access enum values 0 Answers


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