Hi guys I'm trying to build a list based quest system in unity. Everything is working except this bit of code here:
for (int i = currentPoints; i >= 0; i++)
{
if (Quests[i].pointsRequired <= currentPoints)
{
Debug.Log("ping");
currentQuestid += 1;
}
}
I'm not too sure why but any suggestions or help would be great thanks!!
Full script:
public class questManager : MonoBehaviour { public int currentQuestid;
[System.Serializable]
public class Quest
{
public int pointsRequired;
public GameObject objects;
public int questID;
public string objective;
public string pointName;
public string info;
public float waitTime;
public bool enabled = false;
public Text objectiveText;
public Text remainingText;
public Text infoText;
}
public Quest[] Quests;
public int currentPoints;
public float currentdelay;
public int required;
void Start()
{
currentQuestid = 0;
currentPoints = 0;
for (int i = 0; i < Quests.Length; i++)
{
Quests[i].objectiveText.text = Quests[i].objective;
}
}
void Update()
{
for (int i = 0; i < Quests.Length; i++)
{
Quests[i].objects.SetActive(i == currentQuestid);
}
for (int i = 0; i < Quests.Length; i++)
{
Quests[i].remainingText.text = Quests[i].pointName + ": " + currentPoints + "/" + Quests[i].pointsRequired;
}
for (int i = currentPoints; i >= 0; i++)
{
if (Quests[i].pointsRequired <= currentPoints)
{
Debug.Log("ping");
currentQuestid += 1;
}
}
}`
Simple typo in for loop:
for (int i = currentPoints; i >= 0; --i)
{
//Be safe
if (Quests.Length <= i)
continue;
if (Quests[i].pointsRequired <= currentPoints)
{
Debug.Log("ping");
currentQuestid += 1;
}
}
Specifically, you want to decrement i, not increment it.