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
1
Question by Christopher K. · Dec 22, 2010 at 02:29 PM · aifinite-state-machine

Unknown Identifier in Finite State Machine

Hey all, I'm having trouble implementing a Finite state machine and every piece of documentation I've seen seems deliberately vague. Basically, my code's turning up an Unknown Identifier error in lines 19, 21, 23, and 25(case X). Can someone explain to me why this error is appearing and how to rectify it? Also, how do I go about changing the state from within the functions called from Update?

var speed = 10; var minspeed = 2.5; speed *= Random.value; speed += minspeed; var shipTarget : Transform;

enum state { WAITING, SEEKING, ATTACKING, MOVING }

function Update () { if (Time.timeScale > 0) { if (transform.position.y == 0) { switch (state) { case WAITING: shipWait(5); case SEEKING: facePlayer(shipTarget); case ATTACKING: fire(); case MOVING: moveForward(); }

     }
 }

}

function shipWait (waitTime : float) { //Waits up to 5 seconds. yield WaitForSeconds(Random.value * waitTime); }

function moveForward () { //Simply moves forward transform.Translate (0, 0, speed * Time.deltaTime); }

function facePlayer (target : Transform) { //Turns to face the player if (shipTarget) { // Look at and dampen the rotation var rotation = Quaternion.LookRotation(shipTarget.position - transform.position); transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime); } }

function fire () { gameObject.SendMessage("Shoot", SendMessageOptions.DontRequireReceiver); }

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 Proclyon · Dec 22, 2010 at 03:11 PM 0
Share

We can not really see code lines by the way, it's pretty obvious , but in a 3000 line script........ well it's get tricky ;)

2 Replies

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

Answer by Statement · Dec 22, 2010 at 02:54 PM

2 errors.

  • You're trying to switch on a type (state is a type, not a variable).
  • You must include the enum type in your case switches. It is not sufficient to write "WAITING". It should be "state.WAITING". In other languages like C++ you dont write the enumeration type strongly like in C#/JS.

You probably also should use break; for each of your cases or your code would "fall through" the next case(s).

enum State { // Renamed type state to State. WAITING, SEEKING, ATTACKING, MOVING }

var state : State; // Need to declare a variable.

function Update () { if (Time.timeScale > 0) { if (transform.position.y == 0) { switch (state) { case State.WAITING: // Need to be written ENUMTYPE.ENUMMEMBER shipWait(5); break; // Probably want to break. case State.SEEKING: facePlayer(shipTarget); break; case State.ATTACKING: fire(); break; case State.MOVING: moveForward(); break; }

     }
 }

}

Comment
Add comment · Show 8 · 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 Proclyon · Dec 22, 2010 at 02:56 PM 1
Share

HEY HEY HEY ! I was just posting that! +1 for $$anonymous$$d reading :(

avatar image Statement · Dec 22, 2010 at 02:59 PM 1
Share

That's because I'm State(ment).

avatar image Christopher K. · Dec 22, 2010 at 03:00 PM 0
Share

Awesome! This fixed everything! Thanks for the help!

avatar image Proclyon · Dec 22, 2010 at 03:00 PM 0
Share

$$anonymous$$eh it's even quite a terse statement! $$anonymous$$udos, I was going for my usual wall of text

avatar image Mickydtron · Dec 22, 2010 at 04:22 PM 1
Share

I usually wait until it seems that noone will contribute a quick answer and then give some sort of kludgy verbose workaround thing. I mostly learn coding practices by looking at others, so most of what I give that is good is ultimately derived from someone else. Not directly, but in a "shoulders of giants" sort of way. Anyway, this code just got added to my mental library, so thanks. Actually, I've picked up quite a few things from both of you, so many thanks.

Show more comments
avatar image
1

Answer by Justin Warner · Dec 22, 2010 at 02:31 PM

You need a break statement I believe, that's why they don't let us use switch statements in college... lol.

Comment
Add comment · Show 4 · 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 Christopher K. · Dec 22, 2010 at 02:37 PM 0
Share

I'm not sure I understand. Where do I need a break statement?

avatar image Mike 3 · Dec 22, 2010 at 02:49 PM 0
Share

case Something: Code; Break; (on seperate lines of course)

avatar image Christopher K. · Dec 22, 2010 at 02:50 PM 0
Share

Just tried that, still giving me the $$anonymous$$ Identifier error.

avatar image Statement · Dec 22, 2010 at 04:33 PM 1
Share

I'll have a break later ;)

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

No one has followed this question yet.

Related Questions

Getting my object to rotate and move at random 1 Answer

Using coroutines with a Finite State Machine help 2 Answers

Basic AI finite state machine: where to put decision logic? 2 Answers

AI script problems, interaction with grenade not working, need help. 2 Answers

Ai Zombie Melee Attack script. 5 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