Question by
holdingjupiter · Sep 18, 2015 at 08:23 PM ·
arraysloopsquests
Why does this loop throw an error if int questComplete rises above 1?
Now I'm definitely still learning about how loops work so it's a bit of voodoo I have to throw into each code, but I can't for the life of me tease out why this script wouldn't work on two or even infinite quests. Little help?
using UnityEngine;
using System.Collections;
public class completedQuests : MonoBehaviour {
private questLogic mainScript;
public baseQuest[] finishedQuests;
public bool hasRun;
private int incrementChecker;
public int questsComplete;
public int orderCompleted = 0;
// Use this for initialization
void Start () {
mainScript = GetComponent<questLogic>();
incrementChecker = questsComplete;
}
// Update is called once per frame
void Update () {
if(questsComplete != incrementChecker){
finishedQuests = new baseQuest[questsComplete];
for(int i = 0; i < mainScript.allQuests.Length; i++){
if(mainScript.allQuests[i].currentStatus == mainScript.allQuests[i].questLength){
error line==> finishedQuests[orderCompleted] = mainScript.allQuests[i];
orderCompleted++;
}
}
incrementChecker = questsComplete;
}
}
}
Comment
And what is the error message? I'm assu$$anonymous$$g it's an array index out of range or something? Add a Debug.Log before the line you indicate as the error and display the value of orderCompleted. It's likely larger than your finishedQuests array length.
Thanks! I just rewrote the whole program and since it was cleaner it was easier to solve, but it's good to know what was wrong with the original.