Search Unity

Enum inside a System.Object Class?

Discussion in 'Scripting' started by shawn, Dec 15, 2007.

  1. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    I would like to expose an enum to something using a System.Object class, but I'm having difficulties putting any enum inside of the class. It seems the only way to use an enum is inside a monobehavior. The following segment of code seems to be invalid in unity and I'm not exactly sure why. If anyone could enlighten me, that would be fantastic.

    Code (csharp):
    1.  
    2. class CameraData extends System.Object
    3. {
    4.     enum CameraStates
    5.     {
    6.         Follow = 0,
    7.         ChainFollow = 1,
    8.         SlowRotateAround = 2
    9.     }
    10.     // stuff ...
    11. }
    12.  
     
  2. spawrks

    spawrks

    Joined:
    Nov 16, 2007
    Posts:
    89
    what message are you getting back?
     
  3. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    Code (csharp):
    1. Assets/ Scripts/Managers/CameraData.js(3) error BCE0044: expecting }, found 'enum'.
    2. Assets/ Scripts/Managers/CameraData.js(3) error UCE0001: ';' expected. Insert a semicolon at the end.
    3. Assets/ Scripts/Managers/CameraData.js(5) error BCE0044: expecting :, found '='.
    4.  
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Enums are defined as separate objects in the same way classes are. You can define them both in the same file, though.

     
  5. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    I use code like this all the time. Gives a lovely little drop down selection thingy inside unity. Gotta love it.

    Code (csharp):
    1.  
    2. public class LanguageController : MonoBehaviour {
    3.  
    4.     public enum Language {English, Danish};
    5.     public Language language;
    6.  
    7. }
    8.  
    Think you might be missing the ; in your code example after the enum declaration.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The ; is actually not necessary after an enum declaration, in the same way that you don't need one after a class declaration. Your example is the same thing as

    Code (csharp):
    1. enum Language {English, Danish}
    2. var language : Language;
    in Javascript, which of course works fine but isn't really the issue here. :)

    --Eric