Having Issues with getting values from a list.

Hey guys,

So what I have is two scripts UIManager and QuestManager. The QuestManager instantiates a total of 26 quests in the list quests. Now my theory is once a quest is finished it is added to the finishedQuests list. Thus the quest that is always at index 0 in the quest list should always be the next quest (as they are chronologically listed). Thus, I want to display the quest name and description in a text field for index 0 always. However I keep on getting this error once the first quest enters QuestState.Finished:

The Error:

The Line I Get The Error:

    public void UpdateUIInformation()
    {
        questName.text = questManager.quests[0].questName.ToString();
        questDesc.text = questManager.quests[0].questDescription.ToString();
    }

This error even persists when I change the index to any number other than 0, but how is this the case when I have 26 elements in the list?

Any help is very much appreciated! :slight_smile:

UIManager

public class UIManager : MonoBehaviour
{

    public Text questName;
    public Text questDesc;
    public Text totalGoldStars;

    public QuestManager questManager;

    private void Start()
    {
        SetupUIInformation();

        //CalculateTotalGoldStars();
    }

    private void Update()
    {

    }
   
    // Setup the value of the current quest name and description on game start Start().
    private void SetupUIInformation()
    {
        questName.text = questManager.quests[0].questName.ToString();
        questDesc.text = questManager.quests[0].questDescription.ToString();
    }

    // Update the value of the current quest name and description during gameplay Update().
    public void UpdateUIInformation()
    {
        questName.text = questManager.quests[2].questName.ToString();
        questDesc.text = questManager.quests[2].questDescription.ToString();
    }

QuestManager

 public List<Quest> quests = new List<Quest>();
    public List<Quest> finishedQuests = new List<Quest>();

    public UIManager uiManager;

    private void Start()
    {
        // Check to see if no quests exist in the list before instantiating a list of quests.
        if (quests.Count == 0)
        {
            InstantiateQuests(); // Create a bunch of quest constructs.
        }
        else
        {
            return;
        }
    }

    private void Update()
    {
        CheckCurrentQuestStatus();
    }

    // Check if the current quest is complete.
    public void CheckCurrentQuestStatus()
    {
        if(quests[0].questState == QuestState.Finished && !finishedQuests.Contains(quests[0])) // Check to see if the current quest is finished and is not already contained in the finished quests list.
        {
            finishedQuests.Add(quests[0]); // Add the finished quest to the finished quest list.

            if(quests.Contains(quests[0])) // Check to see if the current finished quest is contained in the quests list.
            {
                quests.Remove(quests[0]); // Remove the finished quest from the quest list.
            }
            uiManager.UpdateUIInformation();
        }
        else
        {
            return;
        }
    }

Really silly mistake, nevermind.

I Didn’t reference the UI script in the inspector :stuck_out_tongue: