Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to make this script more dynamic

Discussion in 'Scripting' started by kdarius43, Jan 28, 2019.

  1. kdarius43

    kdarius43

    Joined:
    Mar 16, 2015
    Posts:
    170
    Hello All,

    So at the end of one of my projects I have a survey. Below you can see my code. Essentially I have 5 questions each with 5 answers. So i could create 5 scripts but just to learn how to do it right I wanted to just use one script. So if you look at my code I would like to put this script in each of the questions. The public strings make it so each script will hold the answers to each question. The issue i have is how do i make the line
    submitedRegistration.questionOneAnswer = AnswerA;
    dyanamic. How could I make submitedRegistration.questionOneAnswer public or something so if i attach this script to questions to it would be submitedRegistration.questionTwoAnswer and not .questionOneAnswer

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class QuestionAnswered : MonoBehaviour {
    7.  
    8.     public SubmitRegistration submitedRegistration;
    9.  
    10.     public string AnswerA;
    11.     public string AnswerB;
    12.     public string AnswerC;
    13.     public string AnswerD;
    14.     public string AnswerE;
    15.  
    16.     public void PickAnswer(string answerPicked)
    17.     {
    18.         if(answerPicked == "A")
    19.         {
    20.             submitedRegistration.questionOneAnswer = AnswerA;
    21.         }
    22.         else if(answerPicked == "B")
    23.         {
    24.             submitedRegistration.questionOneAnswer = AnswerB;
    25.         }
    26.         else if (answerPicked == "C")
    27.         {
    28.             submitedRegistration.questionOneAnswer = AnswerC;
    29.         }
    30.         else if (answerPicked == "D")
    31.         {
    32.             submitedRegistration.questionOneAnswer = AnswerD;
    33.         }
    34.         else if (answerPicked == "E")
    35.         {
    36.             submitedRegistration.questionOneAnswer = AnswerE;
    37.         }
    38.     }
    39. }
    40.  
    Thank you
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Create a question class
    Create a collection of questions
    Create a collection of chosen answers
    Display question, add the index of the answer they picked to the chosen answers collection.
    Then when they are done, you have all the answers they chose in a collection in the order they picked.