Hi again everyone. I have this error appear when my games calls a specific if statement. i did research online and iv’e been able to muster up that it keeps pulling a negative number from from a list of random attacks I have which causes a crash sometimes. would anyone know how to resolve this?
Also, what I’m trying to do here is if the attack that is chosen at random is “Ranged” it does one “if” statement.
Below I’ll leave the section of code that this corresponds to.
private IEnumerator TimeForAction()
{
if (actionStarted)
{
yield break;
}
actionStarted = true;
if (CSM.PerformList[0].chosenAbility.aniStance == "Hit")
{
Vector3 playerPosition = new Vector3(PlayerToEngage.transform.position.x, PlayerToEngage.transform.position.y, PlayerToEngage.transform.position.z - 1.5f);
while (MoveTowardsPlayer(playerPosition))
{
yield return null;
}
yield return new WaitForSeconds(0.5f);
DoDamage();
Vector3 firstPosition = startPosition;
while (MoveTowardsStart(firstPosition))
{
yield return null;
}
}
if (CSM.PerformList[0].chosenAbility.aniStance == "Ranged")
{
yield return new WaitForSeconds(0.5f);
DoDamage();
}
CSM.PerformList.RemoveAt(0);
CSM.combatStates = CombatStateMachine.PerformAction.Wait;
actionStarted = false;
cur_bar = 0f;
currentState = TurnState.Processing;
}
My quick guess is that you are trying to access list element #0 of an empty list. If that’s the problem, either first check whether the list is empty (and do something different if it is), OR if you need for there to be something in the list, then fix however you are generating the list so that the list is never empty.
I think it’s calling for a number “Two” when in reality it’s 0-1. Is there any way to keep it within that range? Maybe subtracting the number it’s calling by 1?
What do you mean “should be”? Do you have an actual error or not?
Lines 9 and 25 are both using a literal zero for the index, so there’s no possible way they are accessing index #2. The only direct way those exact lines would throw an index-out-of-bounds error is if your list is empty at the time.