When an Agent priority / goal changes, you will need to tell the Agent to Update the goal's cache.
You can do so by Adding, Changing or Removing a goal and then call Agent.UpdateGoalOrderCache();
public class DynamicGoalExample : MonoBehaviour
{
public Agent Agent;
private void ChangePriority(Goal goal, int priority)
{
goal.Priority = priority;
Agent.UpdateGoalOrderCache();
}
private void AddGoal(Goal goal)
{
Agent.Goals.Add(goal);
Agent.UpdateGoalOrderCache();
}
private void RemoveGoal(int goalId)
{
Agent.Goals.RemoveAt(goalId);
Agent.UpdateGoalOrderCache();
}
// Alternatively, you can just add a Goal Field
public Goal CreateGoal(string key, int priority)
{
return new Goal
{
StateType = EStateType.Text,
StringValue = key,
Priority = priority
};
}
}
When and how you update your goals should be up to your project but here is an example.
Note, this is an example and won't compile as there is no Character.
public class MiniBoss : MonoBehaviour
{
public DynamicGoalExample DynamicGoalExample;
private void Awake()
{
// When a character take damage, the first goal priority changes.
Character.OnTakeDamage += () => {
DynamicGoalExample.ChangePriority(Agent.Goals[0], Random.Range(0,25));
};
}
}