- Home /
Trigger AI character when certain condition is met
I am trying to trigger the AI character only when the player commits a mistake. So I have a bool variable which is initially false and I want the AI character to start chasing the player when the bool variable becomes true. But the problem is even though I declare it as public and static, somehow the the AICharacterControl.cs script doesn't have access to the variable. Second thing I tried is the Singleton example ( given here : https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data ) but even that doesn't work. Can anyone give tips as to how I can have the boolean variable which is truly accessible at the AICharacterControl script so that I can trigger it? Thanks.
Answer by LiloE · Mar 14, 2017 at 09:57 AM
You can set and clear the Target
Property of the AICharacterControl
class.
When this property is null - the agent stops moving. When it is set, the agent will chase the target.
So, all you really need to do, is keep the target null in the beginning and set it when the player commits a mistake.
Thanks a lot for your answer. The problem is that my 'Player.cs' script (which has the bool variable to decide if the player commits a mistake or not) is in a different folder of Assets whereas the AICharacterControl.cs script is in a subfolder of StandardAssets (StandardAssets->Characters->ThirdPersonCharacter->Scripts). One possible solution which works is to keep all the scripts for all the scenes in the later folder location . But is that sustainable? Is there a better way out? Thanks!