- Home /
Index out of range inside IEnumerator
This is my first time using IEnumerator, and my project has been working well so far in terms of the timing of the numerical changes I want. But I've been having an error that I'm having difficulty debugging and can't find a lot of relevant questions on, and I'm not sure if the IEnumerator is why.
Basically, I created a variable called "activeCharacterState" that is either 0,1, or 2 depending on what buttons are pressed by the player. I also have several arrays that I want to use "activeCharacterState" to navigate through, by stating something like "characterShieldValue[activeCharacterState]". It seems like in most cases this works correctly, giving me first, second, or third element of the array when activeCharacterState is 0,1,or 2. However, on the bolded lines when I use "characterShieldValue[activeCharacterState]" or "characterHealth[activeCharacterState]", I get the error "Array index is out of range." I assume this means that "characterShieldValue[activeCharacterState]" is trying to reach an array element outside of the array's size, however, I see now way activeCharacterState could have gotten outside of 0,1, or 2 all of which are in the range of the array.
Here's my full code, lines of giving the error are bolded. My arrays are in italics and I change "activeCharacterState" in the "ChangeCharacterState" function above the Update function. I can elaborate if anybody needs me to. Thanks!
private IEnumerator coroutine;
private IEnumerator coroutineTimer;
public Text enemyHealthText;
public Text playerHealthText;
public Text shieldHealthText;
public Text shieldTotalText;
public Text timer;
public double enemyHealthValue;
//public int playerHealthValue;
//public double shieldHealthValue;
public int shieldTotalValue;
public int timerValue;
public int activeCharacterState;
public int[] characterHealth;
public int[] characterHealthTotal;
public int[] characterHealingRate;
public int[] characterShieldTotal;
public float[] characterShieldValue;
public int[] characterShieldTotalValue;
public double[] characterAttackRate;
public int[] characterSpecial;
void Start () {
coroutine = changeHealthByTime();
coroutineTimer = changeTimer();
StartCoroutine(coroutine);
StartCoroutine(coroutineTimer);
*characterHealth = new int[3] { 100, 150, 100 };
characterHealthTotal = new int[3] { 100, 150, 100 };
characterHealingRate = new int[3] { 1, 1, 1 };
characterShieldTotal = new int[3] { 2, 2, 4 };
characterShieldValue = new float[3] { 20, 20, 20 };
characterShieldTotalValue = new int[3] { 20, 20, 20 };
characterAttackRate = new double[3] { 0.6, 0.3, 0.3 };
characterSpecial = new int[3] { 1, 2, 3 };*
}
private IEnumerator changeTimer()
{
while(true)
{
yield return new WaitForSeconds(1f);
{
if (timerValue > 0)
timerValue--;
else
{
timerValue = 10;
if (characterShieldValue[activeCharacterState] > 0)
{
characterShieldValue[activeCharacterState] = 0;
}
else
{
if (characterHealth[activeCharacterState] > 80)
{
characterHealth[activeCharacterState] -= 80;
}
else
{
characterHealth[activeCharacterState] = 0;
}
}
}
}
yield return null;
}
}
private IEnumerator changeHealthByTime()
{
while (true)
{
if (Input.GetKey("up"))
{
if (characterHealth[activeCharacterState] < characterHealthTotal[activeCharacterState] && characterShieldValue[activeCharacterState] <= 0)
{
characterShieldValue[activeCharacterState] = 0;
yield return new WaitForSeconds(.0005f);
{
characterHealth[activeCharacterState]++;
}
}
if (characterHealth[activeCharacterState] < characterHealthTotal[activeCharacterState] && characterShieldValue[activeCharacterState] > 0)
{
yield return new WaitForSeconds(.0005f);
{
characterHealth[activeCharacterState]++;
}
yield return new WaitForSeconds(.01f);
{
characterShieldValue[activeCharacterState] -= 0.5f;
}
}
if (characterHealth[activeCharacterState] == characterHealthTotal[activeCharacterState] && characterShieldValue[activeCharacterState] > 0)
{
yield return new WaitForSeconds(.01f);
{
characterShieldValue[activeCharacterState] -= 0.5f;
}
}
}
else
{
**if (characterHealth[activeCharacterState] > 0 && characterShieldValue[activeCharacterState] <= 0)
{
characterShieldValue[activeCharacterState] = 0;**
yield return new WaitForSeconds(.01f);
{
characterHealth[activeCharacterState]--;
}
if (enemyHealthValue > 0 && Input.GetKey("down"))
{
yield return new WaitForSeconds(.01f);
{
enemyHealthValue -= 0.3;
}
}
}
**else if (characterHealth[activeCharacterState] > 0 && characterShieldValue[activeCharacterState] > 0)**
{
yield return new WaitForSeconds(.01f);
{
characterShieldValue[activeCharacterState] -= 0.5f;
}
if (enemyHealthValue > 0 && Input.GetKey("down"))
{
yield return new WaitForSeconds(.01f);
{
enemyHealthValue -= 0.3;
}
}
}
else if (characterHealth[activeCharacterState] <= 0)
{
characterHealth[activeCharacterState] = 0;
}
}
yield return null;
}
}
void UpdateCharactersHealth()
{
enemyHealthText.text = enemyHealthValue.ToString("000");
playerHealthText.text = characterHealth[activeCharacterState].ToString();
shieldHealthText.text = characterShieldValue[activeCharacterState].ToString("00");
shieldTotalText.text = shieldTotalValue.ToString();
timer.text = timerValue.ToString();
}
public void ChangePlayerHealth(int boostHealthValue)
{
if (Input.GetKey("space") && characterHealth[activeCharacterState] < characterHealthTotal[activeCharacterState])
{
characterHealth[activeCharacterState] += boostHealthValue;
}
}
public void CreateShield (int shieldAmount)
{
if (Input.GetKeyDown("left") && characterHealth[activeCharacterState] > 20 && shieldTotalValue > 0)
{
characterShieldValue[activeCharacterState] += shieldAmount;
characterHealth[activeCharacterState] -= 20;
shieldTotalValue--;
}
}
void ChangeCharacterState()
{
if (Input.GetKey(KeyCode.Alpha1))
{
activeCharacterState = 0;
}
if (Input.GetKey(KeyCode.Alpha2))
{
activeCharacterState = 1;
}
if (Input.GetKey(KeyCode.Alpha3))
{
activeCharacterState = 2;
}
}
void Update()
{
UpdateCharactersHealth();
ChangePlayerHealth(1);
CreateShield(20);
ChangeCharacterState();
print(activeCharacterState);
}
Your answer
Follow this Question
Related Questions
Audio loop half working 0 Answers
Resizable Array of Classes? 2 Answers
Array index out of range... but it is in range? 1 Answer
whats wrong with this script 2 Answers