IndexOutOfRangeException - beginner. Please help!

Hello everyone,

I am trying to get this script to work, but am having some issues. I have some enemies spawn (I have limited it to one whilst I try to get the to work to check the Debug.Log values) and as soon as they are in range, I call the function Turn() below. The Script works fine until this line (57):

charlist_=enemies*;_
Error as below:
IndexOutOfRangeException: Index was outside the bounds of the array.
(wrapper stelemref) System.Object.virt_stelemref_sealed_class(intptr,object)
BattleStats.Turn () (at Assets/BattleStats.cs:57)
CameraMovement.Update () (at Assets/CameraMovement.cs:70)
and all of the Debug. Logs before this appear to be correct. And enemies.Length > 1, so enemies[0] should be fine. I am guessing something is wrong with me writing charlist[0], but I don’t know why. enemies[] is the list of all enemies and charlist[] is a list of all characters (including all enemies).
If someone could explain that would be really helpful. Thanks.
_
------_
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BattleStats : MonoBehaviour
_
{_
_
public GameObject enemies;_
_
public GameObject player;_
_
public int Ambusha=0;_
_
public int Ambushb=0;_
_
public bool Ambush=false;_
_
public bool startfight = true;_
_
public GameObject charturn;_
_
public GameObject charlist;_
_
public GameObject Temphighest;_
_
public int charactersNumber;_
_
public Canvas battleUI;_
_
public GameObject TurnText;_
_
// Start is called before the first frame update*_
void Start()
{
}
// Update is called once per frame
void Update()
{
}
* public void Turn()*
* {*
* enemies= GameObject.FindGameObjectsWithTag (“Enemy”);*
* player = GameObject.FindGameObjectsWithTag (“Player”);*
* if (startfight == true){*
* for (int i=0; i < enemies.Length; i++)*
* {*
* Debug.Log(“Hello”);*
* Debug.Log(enemies.Length);*
_ Debug.Log(enemies*.GetComponent ().charname);
Ambusha += enemies.GetComponent().luck + enemies.GetComponent().speed;
Debug.Log(Ambusha);
charlist=enemies;
Debug.Log(charlist.GetComponent ().charname);
}
for (int j=0; j < player.Length; j++)
{
Debug.Log(“Hello2”);
Ambushb += player[j].GetComponent().luck + player[j].GetComponent().speed;
charlist[enemies.Length+j+1]=player[j];
Debug.Log(charlist[enemies.Length+j+1].GetComponent ().charname);
}
if (Ambusha > Ambushb)
{Ambush = true;}
startfight = false;
}*_

* charactersNumber= enemies.Length+player.Length;*
* for (int j=0; j<5 ;j++){*

* for (int i=0; i < charactersNumber ; i++)*
* {*
* if (i==0){*
* Temphighest = charlist[0];}*
_ if (charlist*.GetComponent().currentspeed > Temphighest.GetComponent().currentspeed)
{Temphighest = charlist;}*_

* }*
* if (Temphighest.GetComponent().currentspeed<50){*
* for (int k=0; k < charactersNumber;k++){*
* if (charlist[k].GetComponent().affdead == 0)*
* {*
* charlist[k].GetComponent().currentspeed=charlist[k].GetComponent().speed;*
* j = j-1;*
* }*
* }*
* }*
* else {*
* if (Ambush == false){*
* charturn[j]=Temphighest;*
* Temphighest.GetComponent().currentspeed -= 50;}*
* else if(Ambush== true){*
* for(int k=0; k<enemies.Length;k++){*
* charturn[j+k]=enemies[k];*
* Ambush = false;*
* j=enemies.Length-1;*

* }*
* }*
* }*

* }*

* for (int i=1;i<5;i++)*
* {*
* TurnText[0] = GameObject.FindGameObjectWithTag (“Turn”+i);*
* TurnText[0].GetComponent().text= charturn[0].GetComponent().charname;*
* }*
* }*
* void Battle()*
* {*
* }*
}

Good day.

This kind of errors must be solved by you. As error says, some array index calue is out of range. For example, if you try to acess the enemies[5] but enemies array only have 3 elements. The index is out of range of the array.

Bye.