Question about public booleans in this exercise

For some reason I always need to add timer.isAnswering when I want to reference the boolean that is set to public in the Timer script. This is true for all public booleans in the Timer script?

It’s not a big hassle but I’ve propably done something wrong. What could be the cause of this?

I don’t know if it’s useful to copypaste code here;

//
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

using UnityEngine.UI;

public class QuizFunctionality : MonoBehaviour

{

[Header("Questions / Answers")]

[SerializeField] QuestionScriptableObject question;

[SerializeField] TextMeshProUGUI questionTextField;

int correctAnswerIndex;

[Header("Button Sprites")]

[SerializeField] Sprite correctAnswerSpite;

[SerializeField] Sprite defaultAnswerSprite;

[Header("Button Game Objects")]

[SerializeField] GameObject[] answerButtons = new GameObject[4];

bool buttonState = true;

[SerializeField] Color32 wrongAnswerColor;

[Header("Timer properties")]

[SerializeField] Image timerImage;

Timer timer;  



void Start()

{

    timer = FindObjectOfType<Timer>();

    GetNextQuestion();

}



void Update()

{

    // Reduce Timerimage fill relative to time remaining in timerValue

    timerImage.fillAmount = timer.fillFraction;

    // Debugging to see if statement is at any point true;

    if(timer.loadNextQuestion == true)

    {

     Debug.Log("Somewhere Load Next Question Was Set to True it is currently " +timer.loadNextQuestion);  

     // This seems to function properly and is true only on Answer Select (personal note)

    }

    //If timeout then load next question

    if(timer.loadNextQuestion == true)

    {

        Debug.Log("Timeout Getting Next Question");

        timer.hasAnswered = false;

        GetNextQuestion();

        timer.loadNextQuestion = false;

    }

    else if(!timer.hasAnswered && !timer.isAnsweringQuestion)

    {

        DisplayAnswer(-1);

        SetButtonState(false);

    }

}



void GetNextQuestion()

{

    Debug.Log("Getting Next Question");

    SetButtonState(true);

    ResetButtonSprites();

    DisplayQuestionText();

    DisplayAnswersChoices();

}

void DisplayQuestionText()

{

    // Get's the actual questeion and presents it to the player

    Debug.Log("Getting Question Text");

    questionTextField.text = question.GetQuestion();

    Debug.Log("Printed " + questionTextField.text);

}

void DisplayAnswersChoices()

{

    // Loops through all buttons and gets the answer text based on array index number 0-3

    for (int i = 0; i < answerButtons.Length; i++)

    {      

        TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();

        buttonText.text = question.GetAnswerText(i);

    }

}

public void OnAnswerSelected(int playerAnswerIndex)

{

    DisplayAnswer(playerAnswerIndex);

    SetButtonState(false);

    timer.CancelTimer();

}

void DisplayAnswer(int playerAnswerIndex)

{

    Image buttonImage;

    // Check if Answer is Correct

    if(playerAnswerIndex == question.GetCorrectAnswer())

    {

        // Change Correct Answer to yellow

        questionTextField.text = "Correct!";

        buttonImage = answerButtons[playerAnswerIndex].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSpite;

    }

    else

    {

        // Get correct answer index number and store string in variable

        correctAnswerIndex = question.GetCorrectAnswer();

        string correctAnswer = question.GetAnswerText(correctAnswerIndex);

        // Display correct answer to player and change correct answer to yellow.

        questionTextField.text = "Sorry wrong answer! \n the answer was " + correctAnswer;

        buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSpite;

        // Change Player Selected Incorrect Answer to Red

        Image wrongAnswerImage = answerButtons[playerAnswerIndex].GetComponent<Image>();

        wrongAnswerImage.color = wrongAnswerColor;

    }

}

void SetButtonState(bool inputState)

{

    // Loops through all buttons and sets button state to inputted true/false

    Button button;

    Debug.Log("Setting button state to " + inputState);  

    for (int i = 0; i < answerButtons.Length; i++)

    {

        button = answerButtons[i].GetComponent<Button>();

        button.interactable = inputState;

    }

}

void ResetButtonSprites()

    {

        Image buttonImage;

        Debug.Log("Resetting Button Sprites to default");

            for (int i = 0; i < answerButtons.Length; i++)

            {

             buttonImage = answerButtons[i].GetComponent<Image>();

             buttonImage.sprite = defaultAnswerSprite;

             buttonImage.color = Color.white;

            }

    }

}
//

PS.

I mean “timer.” is what I need to add as typing if(isAnswering) returns a red squiggly line. It needs to be

if(timer.isAnswering)

And just to show that they are indeed public
image

Hi,

If you want to access a public member of another object in your current object, you always need a reference to the other object. That’s how object-oriented programming works.

Did this answer your question? :slight_smile:


See also:

Yes actually since I realized in the execise the teacher sets the public booleans inside the Quiz script where as I for some reason set them inside the timer.cs script.

So I’m just an idiot that’s the only problem :smiley:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms