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. Dismiss Notice

Assign a value of an array

Discussion in 'Scripting' started by qiqette, Nov 1, 2016.

  1. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Code (CSharp):
    1. public class Question
    2. {
    3.     public string fact;
    4.     public string answer;
    5. }
    6.  
    7. public class GameManager : MonoBehaviour
    8. {
    9.     private Question[] questions;
    10.     int questioncounter = 0;
    11.     public Text questionText; //The input field where questions are added;
    12.     public void OnClickNextQuestion() //It is called when you press a UI button
    13.     {
    14.         questions[questioncounter].fact = questionText.text; //ERROR HERE (when I press the button)
    15.         //NullReferenceException: Object reference not set to an instance of an object GameManager.OnClickNextQuestion ()
    16.         Debug.Log("This question was the " + questioncounter + "and this is: " + questions[questioncounter].fact);
    17.  
    18.         questioncounter++;
    19.         questionText.text = "";
    20.         if(questioncounter == n_players)
    21.         {
    22.             questionsPanel.SetActive(false);
    23.         }
    24.     }
    25. }
    How can I do what I want?
    Why am I having this issue?

    Thanks in advance :D
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Its because of how arrays work. When you create an array with this line:
    Code (CSharp):
    1. int[] someArray = new int[4];
    Compiler sets aside a small box called someArray. It also sets aside a large box that is 4 * 4bytes long. The small box someArray has inside it an address of the big box. so when you type someArray[0] it looks inside somearray for the big box and goes to the first spot. someArray[1] would look inside someArray and go to the spot, but then go forward the size of one int. So you get all your data

    Now when you do this:
    Code (CSharp):
    1. private Question[] questions;
    The compiler makes the small box questions. But you didn't tell it how many. So inside of questions is just null. It didn't make the big box of how many questions you wanted, because you haven't told it to yet. If you know the size of how many questions there are, just add it to the declaration. If you need an expandable list that you don't know how big it will be I suggest going to Lists:
    https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx


    Your code with Lists:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4.  
    5. public class Question
    6. {
    7.     public string fact;
    8.     public string answer;
    9. }
    10.  
    11. public class GameManager : MonoBehaviour
    12. {
    13.     private List<Question> questions = new List<Question>();
    14.     public Text questionText; //The input field where questions are added;
    15.     public void OnClickNextQuestion() //It is called when you press a UI button
    16.     {
    17.         Question nextQuestion = new Question();
    18.         nextQuestion.fact = questionText.text; //ERROR HERE (when I press the button)
    19.         questions.Add(nextQuestion);
    20.         //NullReferenceException: Object reference not set to an instance of an object GameManager.OnClickNextQuestion ()
    21.         Debug.Log("This question was the " + questions.Count + "and this is: " + questions[questions.Count-1].fact);
    22.  
    23.         questionText.text = "";
    24.         if (questions.Count == n_players)
    25.         {
    26.             questionsPanel.SetActive(false);
    27.         }
    28.     }
    29. }
     
    qiqette likes this.
  3. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Thank you a lot, best answer I got here so far.