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 sdgd · Feb 26, 2013 at 08:36 PM · c#enumhow-tooafa

how to use Enum? C# I'm keep getting Unexpected symbol `enum'

ok I'm bugging my head with this a while

 using System;
 enum Layer{
     Protagonists = 9,
     Antagonists = 10,
     HeadsUpDisplay = 11,
     LevelBoundaries = 13
 };

with or without semicolon just doesn't work

saw many accepted answers tried them all nothing seem to work where am I looking wrong?

and I keep getting same error

 Assets/Scripts/_Class/_Struct/SkillGain.cs(39,24): error CS1525: Unexpected symbol `enum'

from answers I kinda figure out that I don't declare after enum (Layer) what type is it in this case so where do I tell it what is it float/int/GameObject?

I've tried many things:

  • enum float layer

  • enum layer float

both with and without semicolon on end

I've tried to empty the inside {}

and getting same error

EDIT:

and OFC if it's a bug tell me I'm using unity 4.0.1

full code

 using UnityEngine;
 using System.Collections;
 
 
 enum Layer {
     Protagonists = 9,
     Antagonists = 10,
     HeadsUpDisplay = 11,
     LevelBoundaries = 13
 }
 
 
 public struct SkillGain{
     public void GetExpF (float[] SkillExperienceList, int UpdateSkillExperienceNumber, float SkillGainf, char ToolLimb){
         Layer layer = Layer.Antagonists;
         int asdf = layer;
         // we increase skill
         SkillExperienceList[UpdateSkillExperienceNumber] += Mathf.Log(SkillGainf, SkillExperienceList[UpdateSkillExperienceNumber]);
         
         // we increase body strength depending on UpdateExperience number
         if (UpdateSkillExperienceNumber == 19){
             Woodcutting(SkillExperienceList, ToolLimb);
         }
     }
     private void Woodcutting (float[] SkillList, char ToolLimb){
         if (ToolLimb == 'L'){
             SkillList[2] = 10 ;
             SkillList[3] = 1.5f ;
             SkillList[4] = 7 ;
             SkillList[6] = 15 ;
             SkillList[8] = 10 ;
             SkillList[10] = 1.5f ;
             SkillList[12] = 5 ;
             SkillList[15] = 6 ;
         }
         else if (ToolLimb == 'R'){
             SkillList[2] = 10 ;
             SkillList[3] = 1.5f ;
             SkillList[4] = 7 ;
             SkillList[6] = 15 ;
             SkillList[8] = 10 ;
             SkillList[10] = 1.5f ;
             SkillList[12] = 5 ;
             SkillList[15] = 6 ;
         }
         else{Debug.Log("ERROR SkillGain has N limb instead of L/R");}
     }
     private void Attack0 (float[] SkillList, float Strength){//10Kg
         float shoulder = 0;
         float arm = 0;
         float forearm = 0;
         float wraist = 100;
         float tight = 0;
         float leg = 0;
         float back = 0;
         float sum = 0;
         float StartPer = 10;
         sum = shoulder + arm + forearm + wraist + tight + leg + back;
         
         wraist = (wraist/sum)*((Strength/100)*StartPer);
         
         SkillList[0] = 0 ;//"Body Strengths";
             SkillList[1] = 0 ;//"Neck Strength";
             SkillList[2] = 0 ;//"Back Strength";
             SkillList[3] = 0 ;//"Six Pack Strength";
             SkillList[4] = 0 ;//"Left Shoulder Strength";
                  SkillList[6] = 0 ;//"Left Arm Strength";
                 SkillList[8] = 0 ;//"Left Forearm Strength";
                 SkillList[10] = 0 ;//"Left Wraist Strength";
             SkillList[5] = 0 ;//"Right Shoulder Strength";
                 SkillList[7] = 0 ;//"Right Arm Strength";
                 SkillList[9] = 0 ;//"Right Forearm Strength";
                 SkillList[11] = 0 ;//"Right Wraist Strength";
                 
             SkillList[12] = 0 ;//"Left Tight Strength";
                 SkillList[14] = 0 ;//"Left Leg Strength";
                 SkillList[16] = 0 ;//"Left Foot Strength";
             SkillList[13] = 0 ;//"Right Tight Strength";
                 SkillList[15] = 0 ;//"Right Leg Strength";
                 SkillList[17] = 0 ;//"Right Foot Strength";
     }
     private void Attack1 (float[] SkillList, float Strength){
         float shoulder = 90;
         float arm = 100;
         float forearm = 0;
         float wraist = 0;
         float tight = 0;
         float leg = 0;
         float back = 0;
         float sum = 0;
         float StartPer = 60;
         sum = shoulder + arm + forearm + wraist + tight + leg + back;
         
         shoulder = (shoulder/sum)*((Strength/100)*StartPer);
         arm = (arm/sum)*((Strength/100)*StartPer);
         
         // calculation is correct
     }
     private void Attack2 (float[] SkillList){
         
     }
 // Top,    Middle    Bottom
 // 0,    2,        4 right ones
 // 1,    3,        5 left ones
 // 0 10 wraist 100                                                            300    Wraist 100, forarm 90, arm 50
 // 1 60 arm 100, shoulder 90                                                250 shoulder 100, arm 90, tight 10 (other leg 5)
 // 
 // back works here at both I'll test it again
 // 2 100 arm 100, shoulder 90, forearm 35, wraist 20, tight 10, leg 5        400 arm 100, shoulder 70, tight 10, leg 5
 // 3 100 shoulder 90, arm 70, tight 10                                        400    arm 100, shoulder 70, tight 20, leg 5
 //
 // 4 10 wraist 100, forearm 50                                                250    wraist 20
 // 5 60 shoulder 90 arm 80                                                    250    shoulder 70 arm 60 wraist 10
 //
 // thrust 
 // 6 100 shoulder 100, arm 70, forearm 20, wraist 30, (other leg 10)        100 shoulder 80, arm 100, wraist 20, (other leg 10), (other tight 5) back 10
     private void Attack3 (float[] SkillList){
         SkillList[0] = 0 ;//"Body Strengths";
             SkillList[1] = 0 ;//"Neck Strength";
             SkillList[2] = 0 ;//"Back Strength";
             SkillList[3] = 0 ;//"Six Pack Strength";
             SkillList[4] = 0 ;//"Left Shoulder Strength";
                  SkillList[6] = 0 ;//"Left Arm Strength";
                 SkillList[8] = 0 ;//"Left Forearm Strength";
                 SkillList[10] = 0 ;//"Left Wraist Strength";
             SkillList[5] = 0 ;//"Right Shoulder Strength";
                 SkillList[7] = 0 ;//"Right Arm Strength";
                 SkillList[9] = 0 ;//"Right Forearm Strength";
                 SkillList[11] = 0 ;//"Right Wraist Strength";
                 
             SkillList[12] = 0 ;//"Left Tight Strength";
                 SkillList[14] = 0 ;//"Left Leg Strength";
                 SkillList[16] = 0 ;//"Left Foot Strength";
             SkillList[13] = 0 ;//"Right Tight Strength";
                 SkillList[15] = 0 ;//"Right Leg Strength";
                 SkillList[17] = 0 ;//"Right Foot Strength";
     }
     private void Attack4 (float[] SkillList){
         
     }
     private void Attack5 (float[] SkillList){
         
     }
     private void Attack6thrust (float[] SkillList){
         
     }
 }
Comment
Add comment · Show 3
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 hoy_smallfry · Feb 26, 2013 at 08:52 PM 0
Share

How are you using the enum? Can you show the surrounding code?

avatar image sdgd · Feb 26, 2013 at 09:01 PM 0
Share

and don't $$anonymous$$d the most comments those are values I've tried the combat attacks today and strengths they are for designing combat system attack and sta$$anonymous$$a usage...

avatar image Teysz · Jan 05, 2014 at 01:10 PM 0
Share

$$anonymous$$aybe you should add private or public infront of the enum and definitly remove the semicolon (;).

2 Replies

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

Answer by hoy_smallfry · Feb 26, 2013 at 09:06 PM

Your problem is that you are trying to declare the enum in a function scope.

In C#, enums are their own types, just like classes. So you can only declare them in the global scope or within a class scope, like this:

 // GOOD
 enum Enum1
 {
     //...
 };

 class Class1
 {
     // GOOD
     enum Enum2
     {
         //...
     };

     void Function1()
     {
         // BAD
         enum Enum3
         {
             //...
         };
     }
 }
Comment
Add comment · Show 10 · 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 sdgd · Feb 26, 2013 at 09:18 PM 0
Share

yeah thanks got that for now but just seem I don't know how to use them

hm everyone seem to be gone now my problem isn't solved yet as I don't know how to move forward

thanks to dave I've puted enum out of scope but how do I take advantage of them now and make them floats and use them don't know

maybe I'll stick to head calculation numbers for floats in arrays

avatar image hoy_smallfry · Feb 26, 2013 at 09:32 PM 2
Share

Think of them like this: bool have only two values they can possibly be: true and false

char has 256 values they can possibly be: (-128, ..., 0, 1, ... 127)

int has 4,294,967,296 values they can possibly be: (-2147483648, ..., 0, 1, ..., 2147483647)

Enums are a way to define your own type with its own finite values, and that's all they really are for. You define the values in the brackets {} and now your enum cannot be any values other than whats been defined.

avatar image hoy_smallfry · Feb 26, 2013 at 09:51 PM 1
Share

Yes, you can keep changing their values, but their values can only be what you defined in the enum definition:

 // variable mylayer1 is now a HUD
 Layer myLayer1 = Layer.HeadsUpDisplay;

 // now its a antagonist.
 myLayer1 = Layer.Antagonists;

 // you can NOT do this, because that enum value is not part of Layer type:
 myLayer1 = BuildTarget.WebPlayer

You can even combine two values together, in case you're trying to make composites:

 Layer myLayer3 = Layer.Protagonists | Layer.Antagonists;
 
 if (myLayer3.CompareTo(Layer.Protagonists) > 0 &&
     myLayer3.CompareTo(Layer.Antagonists) > 0)
 {
     Debug.Log("he's a good guy and a bad guy!");
 }

 if (myLayer3.CompareTo(Layer.Protagonists) > 0 &&
     myLayer3.CompareTo(Layer.HeadsUpDisplay) > 0)
 {
     Debug.Log("he's a good guy and a HUD! Wow!");
 }
avatar image hoy_smallfry · Feb 26, 2013 at 10:12 PM 2
Share

No, you shouldn't use them as floats. It's possible with casting, but you shouldn't. Just because you are setting them to numbers in the definition doesn't mean they should be treated as numbers. The numbers are there just to differentiate the different enum values.

If you need to use floats, then just define constants for your floats:

 const float Protagonists = 9.0;
 const float Antagonists = 10.0;
 const float HeadsUpDisplay = 11.0;
 const float LevelBoundaries = 13.0;

avatar image hoy_smallfry · Feb 26, 2013 at 10:33 PM 1
Share

Ah, I see. No problem. If you want to refer to them all by a name ins$$anonymous$$d of an index, then you might as well create a structure:

 struct Layers
 {
     public float Protagonists = 9.0f;
     public float Antagonists = 10.0f;
     public float HeadsUpDisplay = 11.0f;
     public float LevelBoundaries = 13.0f;
 }

which is like a class, is sort of like array, but cannot grow to add more things to it. you can make instances of it like you make instances of classes:

 Layers myLayers = new Layers();
 myLayers.Protagonists = 3.0f;
 myLayers.Protagonists = 6.0f;

If you want to be able to grow the container to different sizes and access the elements by a name, you can also use System.Collections.Generic.Dictionary:

 using System.Collections.Generic;

 // access them by strings, the data are floats.
 Dictionary<string, float> myLayers = new Dictionary<string, float>();

 // adding a new value, "Protagonists" with value 9.0f
 myLayers.Add("Protagonists", 9.0f);

 // changing the value.
 myLayers["Protagonists"] = 3.0f;

You can even take the dictionary a step further and use the enum as the keys for the dictionary. That way, you can never add more elements than what was defined in the enum:

 using System.Collections.Generic;
 enum Layer
 {
     Protagonists,
     Antagonists,
     HeadsUpDisplay,
     LevelBoundaries
 };

 // access them by Layer enum, the data are floats.
 Dictionary<Layer, float> myLayers = new Dictionary<Layer, float>();

 // adding 
 myLayers.Add(Layer.Protagonists, 9.0f);

 // changing
 myLayers[Layer.Protagonists] = 3.0f;

Does that help out more?

Show more comments
avatar image
1

Answer by Dave-Carlile · Feb 26, 2013 at 08:59 PM

Where are you declaring the enum?

Edit: After seeing your code, the problem is definitely that you're declaring the enum in the wrong place. They cannot be declared inside of a method.

Here's an example:

 enum Layer { Protagonists = 9, Antagonists = 10, HeadsUpDisplay = 11, LevelBoundaries = 13 }
 
 class TestClass
 {
   Layer layer = Layer.Antagonists;
 }

The enum cannot be declared inside of a method. It's either at the class level or higher. Also, no special namespace (e.g. System) is required.

Comment
Add comment · Show 2 · 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 sdgd · Feb 26, 2013 at 09:04 PM 0
Share

thanks now I did it as you said and got rid of error just gota learn to set it up huh :)

avatar image sdgd · Feb 26, 2013 at 09:10 PM 0
Share

ok am it seems I've done enum but I just seem not to understand it huh

tried it like this:

 Layer layer = Layer.Antagonists;
         layer = 5.5;


 Assets/Scripts/_Class/_Struct/SkillGain.cs(16,17): error CS0266: Cannot implicitly convert type `double' to `Layer'. An explicit conversion exists (are you missing a cast?)

or

 Assets/Scripts/_Class/_Struct/SkillGain.cs(16,21): error CS0266: Cannot implicitly convert type `Layer' to `int'. An explicit conversion exists (are you missing a cast?)

for

 Layer layer = Layer.Antagonists;
         int asdf = layer;

or how do I give GameObjects to it or whatever

...

about your edit yes after I saw your answer enum worked but my new Edit my full code still don't know how to use enum

but I did follow your steps

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How To send Email with C# on Unity 3D 4.1.2? 8 Answers

My Level Take To much Time to Restart (c#) 0 Answers

Problems with enums and switches (C#) 2 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