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[i]=enemies[i];
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[i].GetComponent<Attributes> ().charname);
Ambusha += enemies[i].GetComponent<Attributes>().luck + enemies[i].GetComponent<Attributes>().speed;
Debug.Log(Ambusha);
charlist[i]=enemies[i];
Debug.Log(charlist[i].GetComponent<Attributes> ().charname);
}
for (int j=0; j < player.Length; j++)
{
Debug.Log("Hello2");
Ambushb += player[j].GetComponent<Attributes>().luck + player[j].GetComponent<Attributes>().speed;
charlist[enemies.Length+j+1]=player[j];
Debug.Log(charlist[enemies.Length+j+1].GetComponent<Attributes> ().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[i].GetComponent<Attributes>().currentspeed > Temphighest.GetComponent<Attributes>().currentspeed)
{Temphighest = charlist[i];}
}
if (Temphighest.GetComponent<Attributes>().currentspeed<50){
for (int k=0; k < charactersNumber;k++){
if (charlist[k].GetComponent<Attributes>().affdead == 0)
{
charlist[k].GetComponent<Attributes>().currentspeed=charlist[k].GetComponent<Attributes>().speed;
j = j-1;
}
}
}
else {
if (Ambush == false){
charturn[j]=Temphighest;
Temphighest.GetComponent<Attributes>().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>().text= charturn[0].GetComponent<Attributes>().charname;
}
}
void Battle()
{
}
}
Answer by tormentoarmagedoom · Jan 27, 2019 at 04:57 PM
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.
I managed to solve the error - I hadn't actually specified the size of the array in the Unity inspector, hence it thought the size was 0.
I really appreciate your response though. I am actually having difficulty with my animation, if you are knowledgeable with that?
Your answer
Follow this Question
Related Questions
JSON loading causes WebGL build index out of range exceptions 2 Answers
Array index is out of range (C#)? 1 Answer
problem with index out of bounds 0 Answers
I want to make make a plant to grow after few seconds. 0 Answers
How to GetComponent for SteamVR_TrackedObj when script not attached to controller 0 Answers