• 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 Spider_newgent · Oct 17, 2013 at 07:28 PM · switchenumstate-machinefsm

really weird problem, Enum refusing to change state.

Ok, so I'm using a simple enum and case/switch to control the State of my enemy AI. It's all up and running fine, except for when I'm trying to change to AIState.dying from the AIState.injured.

Having said that, I can change to AIState.dying from other states, and can change from AIState.injured to other states with no problem at all. The problem is only when changing from AIState.injured to AIState.dying.

I've included some, (messy) code below to illustrate the problem.

My enum is set up like t$$anonymous$$s:

 enum AIState
 {
 spawning,
 roaming,
 attacking,
 injured,
 chasing,
 dying
 }

Then I switch between states like t$$anonymous$$s:

 switch (currentState) {
         case AIState.spawning: 
         
             yield WaitForSeconds (1);
             currentState = AIState.roaming;
             
             break;
  
         case AIState.roaming: 
             //make enemy roam around
             
             //checking for proximity to player here
             distance = Vector3.Distance( player.transform.position, transform.position);
         
                 if ( distance < 1.8 && distance > 0.05 ) 
                 {
                     currentState = AIState.chasing;    
                 }
             
             break;
             etc

The state giving me problems is like so:

      case AIState.injured: 
                         myHealth --;
                         
                         if (myHealth <= 0)
                         {
                         Debug.Log("health = 0.");
                          currentState = AIState.dying; //<-- problem is here
                         }

                         currentState = AIState.roaming;
     
     break;

     

And the dying state is here:

 case AIState.dying: 
             Debug.Log("dying.");
             Destroy (gameObject);
             
             break;

Now, if I set: currentState = AIState.dying; from another state, it works fine. the enemy is deleted and "dying" is printed to the console.

Likewise, if I change the conditional statement in the problem state to:

 if (myHealth <= 0)
                             {
                             Debug.Log("health = 0.");
                              currentState = AIState.attacking; //<-- or another AIState                            }

Then again, it works perfectly. The problem is only when changing from injured to dying.

Apologies for the terrible formatting. The problem is pretty straightforward, but it's tough to explain using snippets of code. I'm not precious about my code, if anyone wants the whole enemy script, just say so.

Does anyone know what's causing t$$anonymous$$s strange behaviour?

Thanks

Dan

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 Gjallanhorn · Oct 17, 2013 at 07:43 PM 0
Share

What's happening? You got a error or something, or just doesn't works?

1 Reply

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

Answer by karljj1 · Oct 17, 2013 at 07:37 PM

At line 10 you seem to set the value back straight after changing to dying.

 if (myHealth <= 0)
 {
     Debug.Log("health = 0.");
     currentState = AIState.dying; //<-- problem is here
 }
  
 currentState = AIState.roaming; // LOOK HERE! You set it back straight after setting it to dying. It never stood a chance.
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 Spider_newgent · Oct 17, 2013 at 07:46 PM 0
Share

Hey, thanks for the quick reply.

It's set back to roaming to ensure that if health > 0, it continues following the player.

To my knowledge, setting the state to AIState.dying should delete the object before it can be set back?

avatar image Spider_newgent · Oct 17, 2013 at 07:52 PM 0
Share

and to clarify, if I comment out the line you mention, it still doesn't work. "dying" never gets printed and the object doesn't get destroyed.

avatar image Spider_newgent · Oct 17, 2013 at 08:43 PM 0
Share

Ok, on reviewing it again, you were totally right.

For anyone else encountering anything similar in future, I got around it by making damage conditional, like this:

 case AIState.injured: 
                         Debug.Log("injured.");
                         if (!beenHit){
                         myHealth --;
                         beenHit = true;
                         }
                         else if (beenHit && myHealth > 0){
                         beenHit = false;
                         currentState = AIState.roaming;
                         }
                         
                         if (myHealth <= 0)
                         {
                         Debug.Log("health = 0.");
                          currentState = AIState.dying;
                         }
             break;

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Enum is thought to be a float? 2 Answers

Enum and Variable with similar name resulting in error 1 Answer

Using Playmaker for a game-scope FSM 0 Answers

Switch case using enum javascript 2 Answers

BCE0051 Error 1 Answer


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