Query on FSM (Unityscript)

Hi there

I’ve recently read some of Mat Buckland’s AI book on setting up an FSM including the miner example. I did start to implement this in Unity without too much trouble, but I did have a sense that it was all a bit ‘cumbersome’.

I then started reading some info Jodon Karlik’s website at http://www.codingjargames.com/blog/ which was more unity specific. He suggests a simple way of implementing FSM where each of the states are just child objects, and you enable and disable the children to change states. The OnEnable and OnDisable functions of scripts on the object can then be overriden as the transitions into and out of the states, and the Update function can be used when the state is running. The parent to the child objects could include the ChangeState() function.

This all seems like a great idea to me - I am just trying to learn and do some simple stuff and need a robust way to start creating states and handle transitions. However I then noticed that If I wanted to say call WaitForSeconds from within OnDisable() as a state was exiting (to allow time to do something before the state change), I can’t, as OnDisable cannot be a co-routine. If I try and call WaitForSeconds in a separate function from within OnDisable, I get an error as it won’t execute:

“Coroutine couldn’t be started because the the game object is inactive”

Any suggestions?

you can not do that in ondisable because the game object must be active.

Or more approrpiately: you would need a proxy game object that remains active and acts as the shell to execute the coroutine. The easiest place would likely be your FSM Manager if you have something like that

Johan, you should create a new state that is essentially “timeout before disable”, which is active (so you use OnEnable) until the WaitForSeconds is complete, you then disable it.

Super sorry for the late response, I don’t seem to get notifications when people PM me. Cheers.

Thanks Jodon, better later than never! I had a feeling that I might just need to create a new state. All good.