Formation Code Sense-Check? - Argument Out Of Range Exception

kingboo

New member
Hello,

Apologies, I'm still struggling... Whilst I can move the units in formation a few times and change their leader, I will eventually get an Argument Out Of Range Exception: Index was out of range. Parameter name:index error when changing their position. Would you be able to sense check the below to ensure I have the fundamentals correct?

I have three agents in my game, I can select multiple agents at once and add them into a list. When I select an agent/s, one agent gameObject will be assigned to a Leader variable in the ActorManager script.

Each of my agents has a behavior tree like this:

1612133679476.png1612133729856.png 1612133846757.png

And an Agent script like this:

behavior = GetComponent<Behavior>();
actorManager = GetComponentInParent<ActorManager>();

public void SetDestination(Vector3 destination)
{
if(behavior)
{

if (actorManager.Leader == this.gameObject)
{
behavior.SetVariableValue("Leader", null);
behavior.SetVariableValue("Direction", destination);
}
else
behavior.SetVariableValue("Leader", actorManager.Leader);
}
else
agent.destination = destination;
}

When I select my agent, the Leader value is correctly assigned by the ActorManager script (here's the code snippet):

public GameObject Leader;

Leader = null;

foreach (RaycastHit hit in hits)
if (hit.collider.TryGetComponent(out Actor actor))
{
if (Leader == null)
Leader = actor.gameObject;
else
{
actor.behavior.SetVariableValue("Leader", Leader);
}
selectedActors.Add(actor);
actor.visualHandler.Select();
}

Any feedback is appreciated.
 
When you switch leaders are you also switching the Leader value on the actual leader agent to null? Beyond that in the demo scene there is a "Dynamic Formations" section which shows switching leaders so you can use that code as a reference for switching leaders.
 
Top