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

Resize Serializable List size

Discussion in 'Scripting' started by Hukumuri, Mar 27, 2021.

  1. Hukumuri

    Hukumuri

    Joined:
    Feb 28, 2017
    Posts:
    5
    Currently I have a serializable class called QuestionAnswers with the following :

    public string question;
    public string[] answers;
    public int correctAnswer;

    ******************************************************************
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    [System.Serializable]
    public class QuestionAnswers
    {
    public string question;
    public string[] answers;
    public int correctAnswer;
    }

    *********************************************************************

    In my normal MonoBehaviour script called ListManager I reference the serializable class called QuestionAnswers as QnA:

    public List<QuestionAnswers> QnA;

    *********************************************************************

    public class ListManager : MonoBehaviour
    {

    public List<QuestionAnswers> QnA;

    *********************************************************************

    in the inspector the public List<QuestionAnswers> QnA shows as below

    upload_2021-3-27_18-24-33.png

    My question is how can I change the QnA size value (currently 1, as shown above) and the Answers Size value (currently 4, as shown above) in my ListManger script via an inputfield.text field.

    Note that the structure of the List<QuestionAnswers> QnA(as shown above) should stay the same as the correct answer value is connected to the answers and the answers is connected to the question.

    Example :

    When I increase the QnA size value to say 3 the inspector shows following :

    upload_2021-3-27_18-33-45.png
    (Note the arrows to the left of each element)

    when I click on each element the inspector show the following :

    upload_2021-3-27_18-39-40.png
    (note the arrows to the left of each Answer)

    When I click on the Answers the inspector show the following:
    upload_2021-3-27_18-41-14.png
    (I have added the value 4 next to each Answer in the inspector)

    Just to recap on my question :

    How can I change the QnA size value (currently 1, as shown above) and the Answers Size value (currently 4, as shown above) in my ListManger script via an inputfield.text field.

    Note that the structure of the List<QuestionAnswers> QnA(as shown above) should stay the same as the correct answer value is connected to the answers and the answers is connected to the question.
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'm not really sure I understand the question. Do you mean you want the size of the QnA list and the size of each Answer array to be the same value, and to be able to set that value from a custom text input in the inspector?
    I.E: If you have 4 questions, each question has 4 answers, and if you have 10 questions, each question has 10 answers?

    Or perhaps do you mean you want to globally set the number of questions and answers separately via two custom text inputs?
    I.E: One text input sets the size of the QnA list, and the other sets the size of all the Answer arrays in each QnA list element.

    I don't get what I'm supposed to be noting about the drop-down arrows. Do you instead mean you want to not hide the questions/answers in a drop-down and just display them regularly without hiding them?
     
    Last edited: Mar 27, 2021
  3. Hukumuri

    Hukumuri

    Joined:
    Feb 28, 2017
    Posts:
    5
    Thanks for your reply.
    If I have 4 questions each question should have an options to set how many answers that questions should have.

    So if i have 10 questions I should be able to change the answer size to say 4 and each of the 10 questions should have 4 answers like in a multichoice quiz.

    With this in mind I want to change the QnA size that will determine how many questions there will be and then I want to change the answer size that will determine how many answers each question will have.

    Both the QnA size value and the answer size value I want to change via a inputfield.text field.

    Hope this make more sense.
     
  4. Hukumuri

    Hukumuri

    Joined:
    Feb 28, 2017
    Posts:
    5
    I think I solved the problem.

    This is my code for the serializable list called QnA


     int newsize = 3 (This value will eventually come from an inputfield)   

    [Serializable]
    private class QnA
    {
    public string question;
    public string[] answers;
    public int correctAnswer;
    }


    Then I made a serializedField like this

    [SerializeField] private QnA[] qnaArray;

    In my Start function in just did the following

    public void Start()
    {
    qnaArray = new QnA[newsize];

    }


    So when I run the application the QnA size automatically change to the newsize int value. I will now continue to try and change the answer value through the code and implement the inputfield to replace the newsize int.

    Thanks for everyone's assistance. It is much appreciated.
     
  5. Hukumuri

    Hukumuri

    Joined:
    Feb 28, 2017
    Posts:
    5
    Next question is how do I access the public string[] answer in the serializable class to change its value.