Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Having Issues with getting values from a list.

Discussion in 'Scripting' started by Hotpots, Mar 23, 2017.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    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:
    Code (CSharp):
    1.  
    2.     public void UpdateUIInformation()
    3.     {
    4.         questName.text = questManager.quests[0].questName.ToString();
    5.         questDesc.text = questManager.quests[0].questDescription.ToString();
    6.     }
    7.  
    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! :)

    UIManager

    Code (CSharp):
    1. public class UIManager : MonoBehaviour
    2. {
    3.  
    4.     public Text questName;
    5.     public Text questDesc;
    6.     public Text totalGoldStars;
    7.  
    8.     public QuestManager questManager;
    9.  
    10.     private void Start()
    11.     {
    12.         SetupUIInformation();
    13.  
    14.         //CalculateTotalGoldStars();
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.  
    20.     }
    21.    
    22.     // Setup the value of the current quest name and description on game start Start().
    23.     private void SetupUIInformation()
    24.     {
    25.         questName.text = questManager.quests[0].questName.ToString();
    26.         questDesc.text = questManager.quests[0].questDescription.ToString();
    27.     }
    28.  
    29.     // Update the value of the current quest name and description during gameplay Update().
    30.     public void UpdateUIInformation()
    31.     {
    32.         questName.text = questManager.quests[2].questName.ToString();
    33.         questDesc.text = questManager.quests[2].questDescription.ToString();
    34.     }
    QuestManager
    Code (CSharp):
    1.  public List<Quest> quests = new List<Quest>();
    2.     public List<Quest> finishedQuests = new List<Quest>();
    3.  
    4.     public UIManager uiManager;
    5.  
    6.     private void Start()
    7.     {
    8.         // Check to see if no quests exist in the list before instantiating a list of quests.
    9.         if (quests.Count == 0)
    10.         {
    11.             InstantiateQuests(); // Create a bunch of quest constructs.
    12.         }
    13.         else
    14.         {
    15.             return;
    16.         }
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         CheckCurrentQuestStatus();
    22.     }
    23.  
    24.     // Check if the current quest is complete.
    25.     public void CheckCurrentQuestStatus()
    26.     {
    27.         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.
    28.         {
    29.             finishedQuests.Add(quests[0]); // Add the finished quest to the finished quest list.
    30.  
    31.             if(quests.Contains(quests[0])) // Check to see if the current finished quest is contained in the quests list.
    32.             {
    33.                 quests.Remove(quests[0]); // Remove the finished quest from the quest list.
    34.             }
    35.             uiManager.UpdateUIInformation();
    36.         }
    37.         else
    38.         {
    39.             return;
    40.         }
    41.     }
     

    Attached Files:

  2. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Really silly mistake, nevermind.

    I Didn't reference the UI script in the inspector :p