0

I'm doing a multiple choice quiz game, and im tryinng to highlight the correct answer in green if the wrong answer is clicked. for example if i have A , B , C and D and "A" is the correct answer. i want if i choose "B" to turn B to red color and display the correct answer to green which is in this case "A". Right now i have if i click the right answer it displays green, as i want it. and if i press wrong answer it displays red. what im missing is when i click wrong answer i want to also highlight the correct answer.

Here's my functions:

In the Game controller:

public bool theAnswerIsCorrect;

public bool IsCorrected()
{
    return theAnswerIsCorrect;
}

public void AnswerButtonClick(bool isCorrect)
{
    if (isCorrect)
    {
        Debug.Log("I'm Correct");
        theAnswerIsCorrect = true;
        playerScore += currentRoundData.pointAddedForCorrectAnswer;
        scoreDisplayText.text = "Score: " + playerScore.ToString();


    }

    else
        theAnswerIsCorrect = false;



    // Do we still have questions?
    if (questionPool.Length > questionIndex + 1)
    {
        //questionIndex++;

        UpdateQuestionIndex();
        StartCoroutine(DelayTime(3));
      //  ShowQuestion();
    }

    else
    {
        EndRound();
    }
}

And this is an a class that just holdes my data

public class answerData {

public string answerTxt;
public bool isCorrect;


}

This is being used in the ButtonClick function (the code in the begging of the question) -> this line

        gameController.AnswerButtonClick (AnswerData.isCorrect);

And in the inspector i specify by the bool what is the correct answer

*** NEW SCRIPT HERE "Picking up the Correct button"

// store reference to btn text
public Text answerText;

private answerData AnswerData;
private GameController gameController;

public static AnswerButton _instace;

private void Awake()
{
    _instace = this;
}

// Use this for initialization
void Start () 
{
    gameController = FindObjectOfType<GameController> ();


}

public void Setup(answerData data)
{
    AnswerData = data;
    answerText.text = AnswerData.answerTxt;
}

IEnumerator ReturnButtonColor()
{
    yield return new WaitForSeconds(2.9f);
    GetComponent<Button>().image.color = Color.white;
    Debug.Log("HiB");


}


public void HandleClick()
{
    gameController.AnswerButtonClick (AnswerData.isCorrect);

    if (gameController.IsCorrected())
    {
       GetComponent<Button>().image.color = Color.green;
        Debug.Log("im true");
        StartCoroutine(ReturnButtonColor());


    }

    else
    {
        GetComponent<Button>().image.color = Color.red;
      //  gameController.IsCorrected().image.color = Color.green;
        StartCoroutine(ReturnButtonColor());

    }


}

Thank you

Nanopili
  • 73
  • 12
  • 1
    You'll need to have a way to get a reference to the correct answer button so you can turn it green. Maybe you can add a method to your gameController to return the correct button, then call that inside your else clause. From there, you know how to switch it's color. – Bad Wolf Jan 17 '17 at 07:19

3 Answers3

0

Like using GetComponent<Button>().image.color = Color.green; in the if statement , you could simply add in /*Button A*/.image.color = Color.green; in the else statement.

WQYeo
  • 3,973
  • 2
  • 17
  • 26
  • i basically need the correct answer to turn green if wrong asnwer is clicked. in the 1st if statement it's easy because im just calling the function that checks for me if the answer is correct or not. but im not sure how will it be in the else statement.. – Nanopili Jan 17 '17 at 07:17
  • If you studied generics, simply in `GetComponent – WQYeo Jan 17 '17 at 07:18
  • Im pooling all of my buttons. so actually on the first time i just instantiate them.. so i have no idea how to get "BUTTON A" – Nanopili Jan 17 '17 at 07:19
  • 1
    At the moment where you initialize your buttons set a `Tag` on the `Button` which holds the correct answer – Tomtom Jan 17 '17 at 07:22
  • You will need to get a way to get the reference to the correct answer button. You can create a new method/function that returns the correct button to see what you get. – WQYeo Jan 17 '17 at 07:22
  • i am alrady doing that. the correct button has a public bool that returns if it's correct or not. What i dont know what to do is how to call the correct answer button. as you can see in the function posted i have an if and a else the first if i call function that check for the correct answer. The issue now is in the else, i also want to display the correct answer. Im not sure how to do that in the else, i can't get the BUtton A – Nanopili Jan 17 '17 at 07:25
0

What @BadWolf in the comments is saying is the correct answer. If you need more coding help you have to include more information. How do you determine the Correct Button? Does it have a special name in the Scene? Does the gameController know which button is the correct? It seems gameController knows which Button is the correct one so you should create a method which will look something like:

public Button GetCorrectButton(){
    return theCorrectButton;
}

I don't know the code for how you see if it is the correct button but I'm guessing you have some way where you use the Correct Button. Find the Correct Button and return it in the method GetCorrectButton.

which you will then use in your code like:

public void ButtonClick()
{
    gameController.AnswerButtonClick (AnswerData.isCorrect);

    if (gameController.IsCorrected())
    {
        GetComponent<Button>().image.color = Color.green;
        Debug.Log("im true");
        // StartCoroutine(ReturnButtonColor());
    }
    else
    {
        GetComponent<Button>().image.color = Color.red;
        // Like this:
        gameController.GetCorrectButton().image.color = Color.green;
    }
}

I can be more specific if I get some more information/code to see!

Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32
0

I am assuming that all option button have same script attached.

Create one delegate and register this in your script which is attached to your option button.

Like this

Create delegate and event

#region DELEGATES
public delegate void OnQuestionOptionClicked ();
public static event OnQuestionOptionClicked onQuestionOptionClicked;
#endregion

#region DELEGATE_CALLS
private void RaiseOnQuestionOptionClicked ()
{
    if (onQuestionOptionClicked != null)
        onQuestionOptionClicked ();
}
#endregion

Register it

void OnEnable ()
{
    onQuestionOptionClicked += OnQuestionOptionClicked;
}

void OnDisable ()
{
    onQuestionOptionClicked -= OnQuestionOptionClicked;
}

#region DELEGATE_EVENT_LISTENER
void OnQuestionOptionClicked ()
{
    GetComponent<Button>().interactable = false;
    if (AnswerData.isCorrect){
        GetComponent<Button>().image.color = Color.green;
        Debug.Log("im true");
    }
}
#endregion

Your setup method

public void Setup(answerData data)
{
    AnswerData = data;
    answerText.text = AnswerData.answerTxt;
    GetComponent<Button>().interactable = true;
    GetComponent<Button>().image.color = Color.white;

}

And on button click event

#region BUTTON_CLICK_LISTENER
public void ButtonClick()
{
    gameController.AnswerButtonClick (AnswerData.isCorrect);

    if (!gameController.IsCorrected())
    {
        GetComponent<Button>().image.color = Color.red;
        Debug.Log("im true");
        // StartCoroutine(ReturnButtonColor());
    }

    RaiseOnQuestionOptionClicked ();
} 
#endregion

Hope this solution helps you.;)

Nikunj Rola
  • 156
  • 5
  • ty, but i actually don't understand your solution.. Whenever i click on wrong button i want to display the clicked button red and change the correct button to green. this way i show the user the correct answer before going to th next question – Nanopili Jan 17 '17 at 10:40
  • your solution gives the exact same behavior that i currently have.. my issue is that i can't figure out how to change the correct button to green only IF the user choose the wrong asnwer – Nanopili Jan 17 '17 at 10:46
  • Have you add this code in your project. I mean use this code?? – Nikunj Rola Jan 17 '17 at 10:46
  • What - "gameController.IsCorrected()" Return Can you please share this function? – Nikunj Rola Jan 17 '17 at 10:49
  • it's alrady in the question, it's in the first part of the question, the 1st function – Nanopili Jan 17 '17 at 10:54
  • i think AnswerData.isCorrect refrence variable that holds option text and it is correct or not, and it is in all option button. Am i right?? – Nikunj Rola Jan 17 '17 at 10:55
  • what do u mean option button? and yes AnswerData cariesthe check if it's correct or not and the answer text. and in this line i pass it "gameController.AnswerButtonClick (AnswerData.isCorrect);" – Nanopili Jan 17 '17 at 10:59
  • i have Update my answer please check and let me know if it is working or not. – Nikunj Rola Jan 17 '17 at 11:08
  • it's working but i really dont know how... like in my logic it should't work lol xD can u explain the flow? What i understand is u have an event which get's triggered everytime u click on the button – Nanopili Jan 17 '17 at 11:12
  • now i need to clear the colors and return them to default for next question cuz now when i go to next question the buttons stays red / green – Nanopili Jan 17 '17 at 11:12
  • You can set color of button on start or OnEnable method. Where you set text of option. – Nikunj Rola Jan 17 '17 at 11:16
  • I need to make the other buttons not interactable because when user checks an answer i want him to not be able to click on any other button untill other question show\s up. In your opinion how should i do it? deactivate the whole Manager or just loop through the button pooling and make them not interactable? – Nanopili Jan 17 '17 at 11:16
  • i have update code check it out. Thanks for upvoting my answer ;) – Nikunj Rola Jan 17 '17 at 11:29
  • Thank you for saving me... xD! – Nanopili Jan 17 '17 at 11:31
  • Hey again! if you have some free time would you help me in a other issue im also having :D? if you agree kindly reply so i can create the question – Nanopili Jan 17 '17 at 12:47
  • Sure When i get time i will answer your questions – Nikunj Rola Jan 17 '17 at 13:34
  • Here's the link : http://stackoverflow.com/questions/41698506/unity-remove-the-selected-object-from-random-range – Nanopili Jan 17 '17 at 13:44