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 ToeBeanGames · Jan 15, 2014 at 06:04 AM · c#switchenemy aienum

Problems with enums and switches (C#)

I'm working on my enemy AI here. What I'm trying to do is something similar to the standard character controller, where it just has a current character state enum, but what I'm doing with it is have a switch for said enum where it tells it what animation to play.

 using UnityEngine;
 using System.Collections;
 
 enum EnemyState{IDLE = 0, WALK = 1, RUN = 2, ATTACK = 3, KILL = 4};
 
 public class EnemyBehavior : MonoBehaviour
 {
 
     public AnimationClip enemyIdle;//setting animations
     public AnimationClip enemyWalk;//walking
     public AnimationClip enemyRun;//running/chasing
     public AnimationClip enemyAttack;//attacking player
     public AnimationClip enemyKill;//killing player
     
     public Transform Player;//adds the player to the script
     
     public int WalkingSpeed = 1;//sets the normal walking speed
     public int RunningSpeed = 3;//sets the running (chase player) speed
     public int MaxDist= 15;//maximum distance the player can be before enemy stops chasing the player
     public int MinDist= 7;//how close the player can be to enemy for enemy to start chasing player
     EnemyState currentState;
     
     void Update()//when object enters the Sphere Collider (Chase Range)
     {
          if(Vector3.Distance(transform.position,Player.position) <= MaxDist)//if that object has the tag "Player"
         {
           transform.LookAt(Player);//Looks at the player
             currentState = EnemyState.RUN;
            transform.position += transform.forward*RunningSpeed*Time.deltaTime;//follows the player
         }else if(Vector3.Distance(transform.position,Player.position) >= MaxDist)
         {
             RoamAround ();
         }
         switch (currentState)
         {
             case IDLE:
                 animation.Play ("enemyIdle");
                 break;
             case WALK:
                 animation.Play ("enemyWalk");
                 break;
             case RUN:
                 animation.Play ("enemyRun");
                 break;
             case ATTACK:
                 animation.Play ("enemyAttack");
                 break;
             case KILL:
                 animation.Play ("enemyKill");
                 break;
             default:
                 animation.Play ("enemyIdle");
                 break;
         }
     }
     void RoamAround()
     {
         currentState = EnemyState.IDLE;
     }
 }

What I'm getting is errors like "The name `IDLE' does not exist in the current context" for every single EnemyState. What am I doing wrong? (I've tried adding the enum into the class, but it doesn't work like that either.)

Comment
Add comment · Show 1
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 ToeBeanGames · Jan 15, 2014 at 06:08 AM 0
Share

Okay, never $$anonymous$$d. Apparently I needed to add "EnemyState." before everything in the switch.

2 Replies

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

Answer by morbidcamel · Jan 15, 2014 at 06:07 AM

In C# you have to put the Enum name as part of the qualifier:

 switch(currentState)
 {
   case EnemyState.IDLE:
   ...
   break;
 }
Comment
Add comment · Show 1 · 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 morbidcamel · Jan 15, 2014 at 06:10 AM 0
Share

You also might want to move you enum back into the class or add "public" in front of it. I suggest going through a few scripting tutorials.

avatar image
0

Answer by ammirea · Sep 02, 2019 at 04:12 PM

Make EnemyState public and put it inside EnemyBehaviour and write using static EnemyBehaviour.EnemyState; at the top of the script. This only works with DotNet 4.x +

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

20 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

Related Questions

Problem with my weapons switch statement C# 3 Answers

Changing enum values 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to check if an enum’s case has changed 3 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