Disabling game objects in an array when current conditions are met.

My code is currently creating a question answer set and when the answer matched that of the user input it marked the question correct, adds to the score and moves to the next question. My question is how do I disable a game object attached to the correct question and answer set. I may have this in the wrong place, I will admit that.
I added the public GameObject[ ] hideAnswers; to the class QuestionAnswerSet which allows me to add the game object to the related question and answer. My issue is I don’t know how to tell the code to turn off the game object or where to add that into my code.

I tried to use SetActive in the Submit() function, but I can’t call that without an error. I think I need to tell the code to assign the game object to the current question answer set, but I am not sure where.

Thanks for the help.

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System;

public class gamemanagerscript : MonoBehaviour
{
    public scoremanagertest scoreAccess; //This is so the question control can have access to the scoremanager.
    public GameObject tabletInterface;
    public Button roomButton;
    public int scoreValue;
    public int buttonCount;
    public Text answerresulttext; //This displays after every answer input informing the user of right or wrong answer.
    public InputField userInputField; // the input field the user will type in
    public InputField userInputField2;
    [Serializable]
    public class QuestionAnswerSet
    {
        public string question;
        public List<string> answers;
        public GameObject[] hideAnswers;
    }

    // configured in the Inspector
    public GameObject[] highlights; //Puts the highlight game objects into a list in the inspector.
    public List<QuestionAnswerSet> questionAnswerList; // list of question-to-answer sets
    private int currentQAIndex; // index of the current question-to-answer set
    private QuestionAnswerSet currentQA; // the current question-to-answer set

    private void Start()
    {
        // initialize current question to the first in the list
        if(questionAnswerList.Count > 0)
        {
            currentQA = questionAnswerList[0];
        }

        answerresulttext.text = "";
    }

    void Update ()
    {
        userInputField.text = userInputField.text.ToUpper ();
        userInputField2.text = userInputField2.text.ToUpper ();

        if (Input.GetKey ("escape"))
            Application.Quit ();
    }
       
    public void NextQuestion()
    {
        currentQAIndex++;
        if(questionAnswerList.Count <= currentQAIndex)
        {
            currentQAIndex = 0;
        }
        currentQA = questionAnswerList[currentQAIndex];

        // display question somehow
        string question = currentQA.question;
    }

    public void Submit()
    {
        string[] userAnswers = (userInputField.text + " " + userInputField2.text).Split(',');

        foreach(string userAnswer in userAnswers)
        {
            if (currentQA.answers.Contains (userAnswer))
            {
                answerresulttext.text = "'" + userAnswer +             "' is correct!"; //Displays results on screen for user.
                userInputField.text = ""; //Sets userinput to empty.
                userInputField2.text = ""; //Sets userinput to empty.
                tabletInterface.SetActive (false);
                scoreAccess.AddScore (scoreValue);
                scoreAccess.AddButtonCount (buttonCount);
                for (int i = 0; i < highlights.Length; i++) //For loop checks the highlight list and sets any active to inactive.
                    highlights [i].SetActive (false);
                roomButton.interactable = true;
               
                break;
            }

            else
            {
                Debug.Log (userAnswer);
                answerresulttext.text = "'" + userAnswer + "' is incorrect! The correct answer is: '" + currentQA.answers [0] + "'";
                //tabletInterface.SetActive (false);
                //scoreAccess.AddButtonCount (buttonCount);
                print ("User answer: '" + userAnswer + "' is incorrect! The correct answer is: '" + currentQA.answers[0] + /*"," + currentQA.answers[1] + */"'");
            }
        }
    }

    public void SetQuestion(int index)
    {
        currentQAIndex = index;
        currentQA = questionAnswerList[currentQAIndex];
    }

    public void ActivateSystem ()
    {
        tabletInterface.SetActive (true);
    }

Is there just one HideAnswer GameObject for each question? or does each question have a lot of hideAnswers associated with it. If you only have one object per question it should be:

public class QuestionAnswerSet
    {
        public string question;
        public List<string> answers;
        public GameObject hideAnswers;
    }

Then to turn it off:

if (currentQA.answers.Contains (userAnswer))
            {
                answerresulttext.text = "'" + userAnswer +             "' is correct!"; //Displays results on screen for user.
                userInputField.text = ""; //Sets userinput to empty.
                userInputField2.text = ""; //Sets userinput to empty.
                tabletInterface.SetActive (false);
                scoreAccess.AddScore (scoreValue);
                scoreAccess.AddButtonCount (buttonCount);
                for (int i = 0; i < highlights.Length; i++) //For loop checks the highlight list and sets any active to inactive.
                    highlights [i].SetActive (false);
                roomButton.interactable = true;
                // <== NEW CODE ==>
                currentQA.hideAnswers.SetActive(false);
                // Or if you had more than one HideAnswer object like your original array suggested
                /*  foreach(GameObject oneAnswer in hideAnswers)
                            oneAnswer.SetActive(false);   */
             
                break;
            }
1 Like