- Home /
Finite States Machine (Inheriting question)
I salute the Unity Community!
I was scripting some AI for animals in a game, and I came across a problem you might be able to help me with. I'm taking a FSM approach, and I have an abstract class that contains the FSMState type. The script that I actually attach to the animal is AnimalAI, and it contains a class that inherits from MonoBehaviour, that starts the FSM system, and then a lot of different classes that inherit from FSMState class, that contains the different states logic.
Now, the extension of everything alltogether is a bit uncomfortable to work with, and many times I find myself wanting to have a direct reference to components of the object like gameObject, or transform, and I can't access them directly cause they inherit from FSMState class, not MonoBehaviour.
So what I did was separate the different state classes in different scripts. Now I can't add them to the object, cause they don't inherit from MonoBehaviour, but from FSMState.
Can anyone point out how can I manage to have the different states in different scripts, and still add them all to the object, in order to have direct access to its components without continuously using the GetComponent function, and have a cleaner, better organized code?
Thanks a lot in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220613092812im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Inheritance the best way to modify AI functions? 0 Answers
Bestpractices for statemachines 0 Answers
Instantiate without inheriting from MonoBehaviour 2 Answers
FSM object reference not set to an instance of an object 0 Answers
Cooperating AI bots 0 Answers