ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

poweremil

New member
Hello,
I'm getting this exception and I have been unable to fix it:

Code:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <437ba245d8404784b9fbab9b439ac908>:0)
BehaviorDesigner.Runtime.BehaviorManager.RunParentTask (BehaviorDesigner.Runtime.BehaviorManager+BehaviorTree behaviorTree, System.Int32 taskIndex, System.Int32& stackIndex, BehaviorDesigner.Runtime.Tasks.TaskStatus status) (at <ddb5579d0d23478c9f73ec2f99600079>:0)
BehaviorDesigner.Runtime.BehaviorManager.RunTask (BehaviorDesigner.Runtime.BehaviorManager+BehaviorTree behaviorTree, System.Int32 taskIndex, System.Int32 stackIndex, BehaviorDesigner.Runtime.Tasks.TaskStatus previousStatus) (at <ddb5579d0d23478c9f73ec2f99600079>:0)
BehaviorDesigner.Runtime.BehaviorManager.RunParentTask (BehaviorDesigner.Runtime.BehaviorManager+BehaviorTree behaviorTree, System.Int32 taskIndex, System.Int32& stackIndex, BehaviorDesigner.Runtime.Tasks.TaskStatus status) (at <ddb5579d0d23478c9f73ec2f99600079>:0)
BehaviorDesigner.Runtime.BehaviorManager.RunTask (BehaviorDesigner.Runtime.BehaviorManager+BehaviorTree behaviorTree, System.Int32 taskIndex, System.Int32 stackIndex, BehaviorDesigner.Runtime.Tasks.TaskStatus previousStatus) (at <ddb5579d0d23478c9f73ec2f99600079>:0)
BehaviorDesigner.Runtime.BehaviorManager.Tick (BehaviorDesigner.Runtime.BehaviorManager+BehaviorTree behaviorTree) (at <ddb5579d0d23478c9f73ec2f99600079>:0)
BehaviorDesigner.Runtime.BehaviorManager.Tick () (at <ddb5579d0d23478c9f73ec2f99600079>:0)
BehaviorDesigner.Runtime.BehaviorManager.Update () (at <ddb5579d0d23478c9f73ec2f99600079>:0)

It is an external tree that I set like this:
C#:
behaviorTree.DisableBehavior();
behaviorTree.ExternalBehavior = Behaviors[action];
behaviorTree.EnableBehavior();
Behaviors is a Dictionary containing external trees that I set in the inspector on the GameObject.

It is this specific external tree called "Shoot" where the exception occurs in.
This is how the tree looks like when the exception occurs:

BehaviorScreenshot.png

The Log task used to be a custom task but I replaced it with this since I thought maybe it was what caused the exception but it happens either way.
Sometimes the exception happens the first time it reached this point sometimes it can complete the tree 1-2 times and then get the exception on the 3rd time.

The right Sequence branch is a Behavior Tree Reference in case it is good to know.
 

unicat

New member
Hi, getting the same error here. Simple scene with 1 player and 1 enemy. Using Unity 2019.4.8f1 LTS. Will this be fixed? .
 

Justin

Administrator
Staff member
The problem ended up being caused by the external behavior tree being switched within the OnUpdate loop of the task. The tree should be restructured so it doesn't contain the external behavior tree switch in the middle of the tree execution. I have this exception fixed but to fix the root cause it'll require a tree restructure.
 
Top