Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 UniteMage · May 21, 2011 at 05:41 AM · c#externalkeyword

C# keyword extern

I have enum types that are required in three C# files. One file inherits from one file where the inherited file contains the declaration. Therefore the remaining file also requires a reference to the enum types.

Would I use: "extern public enum enumName" or is there another method?

Thanks CSDG

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TheDemiurge · May 21, 2011 at 05:56 AM

with c#, most declarations / definitions made in one script are seen by the others. but check this if you're having trouble getting something to be recognized elsewhere and you've already made the actual definition public.

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
avatar image
1

Answer by Statement · May 21, 2011 at 09:21 AM

Extern is used when you bind with external dlls, not for referencing an enum within the same project.

Here's an example of how you can access an enum defined inside another class' scope:

 class Foo
 {
     public enum Bar
     {
         A, B, C
     }
 
     void Test()
     {
         Bar bar = Bar.A;
     }
 }

 class Example
 {
     void Test()
     {
         Foo.Bar bar = Foo.Bar.B;
     }
 }

Notice how you had to type out the class name before the enum name to access it from the other class.


Another version would be to put the enum outside the class all together:

 enum Bar
 {
     A, B, C
 }

 class Foo
 {
     void Test()
     {
         Bar bar = Bar.A;
     }
 }

 class Example
 {
     void Test()
     {
         Bar bar = Bar.B;
     }
 }

Notice that we now can use the enum much more easily. All classes can use the enum without having to add any special code around it.

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
avatar image
0

Answer by UniteMage · May 21, 2011 at 01:20 PM

Thanks for the replies.

Still trying to learn the tags.... NOT WORKING.

The problem, there were several classes: a FSMState class, a FSMSystem class, and a NPCControl class. Yet there were two files. The file FSMMachine.cs that contained the two former classes. In this file, I had to use the namespace FSMMachine.

In the other file, the NPCControl.cs contained the keyword "using" for the namespace that I had used. The FSMMachine.cs was not attached to anything, the NPCControl.cs was attached to my NPC GameObject.

The FSMState class was an abstract class, Unity would not allow me to attach it to a GameObject. The FSMSystem class was the Finite State machine. So these two classes were placed in the same file so other GameObjects could have access. This cut down any code redundancy. So all I had to worry about was the NPC control and its states.

The enumeration was also placed in the namespace FSMMachine. By using the namespace, everything was carried over into NPCControl including the enumeration.

Thanks again CSDG

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 TheDemiurge · May 21, 2011 at 06:14 PM 0
Share

abstract classes can't be instantiated as you know, and only classes that derive from $$anonymous$$onoBehaviour can be attached to GameObjects, so the abstract one definitely cannot derive $$anonymous$$B. if one file defines the enum within namespace name {..} , the others should be able to use it, even if they also define more code inside namespace name {..}.

If you've fixed everything, though, please mark your own answer as chosen.

avatar image UniteMage · May 21, 2011 at 09:58 PM 0
Share

Thanks

I am still learning the Unity Engine, $$anonymous$$onoDevelop and C#.... C# is a strange beast. It seems to have incorporated ideas from several program$$anonymous$$g languages. You ought to see me debugging this FS$$anonymous$$. I was pulling my hair out. I finally had to google all the C# PDFs I could find on the internet. The more I learn C#, the more I know this strange beast.

CSDG

avatar image Statement · May 23, 2011 at 02:00 PM 0
Share

@TheDemiurge, you are wrong. The abstract one can definitely inherit from $$anonymous$$onoBehavior. What matters is that $$anonymous$$onoBehavior is inherited at some point in the inheritance chain. Since it is an abstract class, you should create your own states that inherit from that abstract base class, or use any classes that were included. If the enum is in a namespace you aren't using, you have to add using ThatNameSpaceName; to your scripts that want to use that enum.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using external assembly on IOS (ExecutionEngineException) 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

Problem with Write Access 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