How do I check the answer, and move to the next slot only if the first answer is correct? How to only track 1 boolean, and how to reset bool states?

huangapple go评论70阅读模式
英文:

How do I check the answer, and move to the next slot only if the first answer is correct? How to only track 1 boolean, and how to reset bool states?

问题

我理解你的问题,你想要创建一个基于康特广场(生物学概念)的游戏,并需要帮助编写有效的代码来处理答案的顺序,同时确保只影响相关的布尔变量。此外,你还希望在所有答案都正确时重置游戏状态。

以下是你提供的代码的一些关键建议:

  1. 为了在正确的顺序中回答问题,你可以使用一个变量来跟踪当前应该回答的槽,例如 currentSlotToAnswer

  2. Update 方法中,你可以检查当前槽是否已经回答正确,如果是,就切换到下一个槽:

if (answer == firstAnswer && !isCorrect1)
{
    isCorrect1 = true;
    currentSlotToAnswer = GameObject.Find("Slot2");
}
else if (answer == secondAnswer && !isCorrect2)
{
    isCorrect2 = true;
    currentSlotToAnswer = GameObject.Find("Slot3");
}
// 继续处理其他槽...
  1. 当所有答案都正确时,你可以重置游戏状态,例如:
if (isCorrect1 && isCorrect2 && isCorrect3 && isCorrect4)
{
    // 重置所有布尔变量
    isCorrect1 = false;
    isCorrect2 = false;
    isCorrect3 = false;
    isCorrect4 = false;
    allCorrect = false;

    // 重置当前回答的槽
    currentSlotToAnswer = GameObject.Find("Slot1");
}

这些建议应该有助于你改进你的代码,以满足你的需求。如果你需要更多帮助或有其他问题,请随时提问。

英文:

I'm fairly new to Unity and I'm trying to make a punnett square game (Biology concept). Below is an image of the UI:

How do I check the answer, and move to the next slot only if the first answer is correct? How to only track 1 boolean, and how to reset bool states?

There are 4 slots and they're supposed to be answered in order from slot1 (top left), slot 2(top right), slot 3 (bottom left), and slot 4 (bottom right). I'd like to ask your help in writing an efficient code for it so that they could be answered in order. I would also appreciate your help with 2 other problems: How do I track only the answer in slot1 when slot1 is the one being answered, and have the other booleans be unaffected? Lastly, how do I reset the state of the whole punnett square and have the booleans set to false, and slot1 to be the slotToAnswer again?

Here is the line of code I've tried to use

using Mono.Cecil.Cil;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SocialPlatforms;
using UnityEngine.UI;

public class AnswerScript : MonoBehaviour
{
    public bool isCorrect1, isCorrect2, isCorrect3, isCorrect4, allCorrect;
    public QuizManager quizManager;
    public GameObject slotToAnswer;
    public string firstAnswer, secondAnswer, thirdAnswer, fourthAnswer, answer;
    public TextMeshProUGUI dominantText, heteroText, recessiveText;
    public Button dominantButton, heteroButton, recessiveButton;

    public void Start()
    {
        // Initializing the buttons
        dominantButton = GameObject.Find("Dominant").GetComponent<Button>();
        heteroButton = GameObject.Find("Heterozygous").GetComponent<Button>();
        recessiveButton = GameObject.Find("Recessive").GetComponent<Button>();

        // Initializing the text
        dominantText = dominantButton.GetComponentInChildren<TextMeshProUGUI>();
        heteroText = heteroButton.GetComponentInChildren<TextMeshProUGUI>();
        recessiveText = recessiveButton.GetComponentInChildren<TextMeshProUGUI>();

        // Initializing the first slot to answer
        slotToAnswer = GameObject.Find("Slot1");

        isCorrect1 = false;
        isCorrect2 = false;
        isCorrect3 = false;
        isCorrect4 = false;
        allCorrect = false;

    }
    public void Update()
    {

        firstAnswer = quizManager.allele1.text + quizManager.allele3.text;
        if (firstAnswer == "aA")
        {
            firstAnswer = "Aa";
        }
        secondAnswer = quizManager.allele2.text + quizManager.allele3.text;
        if (secondAnswer == "aA")
        {
            secondAnswer = "Aa";
        }
        thirdAnswer = quizManager.allele1.text + quizManager.allele4.text;
        if (thirdAnswer == "aA")
        {
            thirdAnswer = "Aa";
        }
        fourthAnswer = quizManager.allele2.text + quizManager.allele4.text;
        if (fourthAnswer == "aA")
        {
            fourthAnswer = "Aa";
        }

        dominantButton.onClick.AddListener(TexttoDominant);
        heteroButton.onClick.AddListener(TexttoHetero);
        recessiveButton.onClick.AddListener(TexttoRecessive);
        answer = slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text;

        if (answer == firstAnswer)
        {
            isCorrect1 = true;
            slotToAnswer = GameObject.Find("Slot2");
            answer = slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text;
        }
        if (answer == secondAnswer)
        {
            isCorrect2 = true;
            slotToAnswer = GameObject.Find("Slot3");
            answer = slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text;
        } if (answer == thirdAnswer){
            isCorrect3 = true;
            slotToAnswer = GameObject.Find("Slot4");
            answer = slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text;
        } if (answer == fourthAnswer)
        {
            isCorrect4 = true;
            Debug.Log("Congrats!");
            allCorrect = true;
        }
  }
    // Button methods
    public void TexttoDominant()
    {
        slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text = dominantText.text;
    }
    public void TexttoHetero()
    {
        slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text = heteroText.text;
    }
    public void TexttoRecessive()
    {
        slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text = recessiveText.text;
    }
}

In the image, the correct answer for slot1 would be "Aa". As said in the title, I would like to make it so that slotToAnswer will be slot1 until the text inside slot1 is equal to Aa. Once slot1 text is equal to Aa, I would like to have the next slot (slot2) to be the slotToAnswer, and the process to continue until slot4. As of now, it only works when I input only the correct answers and in correct order. When I input the supposed answer for slot2 in slot1 on purpose, the boolean isCorrect2 for slot2 is set to true and the slotToAnswer becomes Slot3 (bottom left square) and everything goes into shambles. I would like to only affect the boolean related to a slot and not the other booleans. Lastly, when they are allCorrect, I would like to reset all values and start again.

I'm sorry if my explanation is hard to understand as I am a beginner. I appreciate all the help you guys will be able to provide! <3

答案1

得分: 0

以下是已翻译的代码部分:

我认为这里最简单的答案是检查`slotToAnswer`的值和实际答案,以确保它们一致。您上面的检查不考虑应该检查哪种情况,因此它会检查每个答案,直到找到一个正确的。

请注意,使用`else if`而不是`if`,以便不管问题如何都不会尝试验证每个答案。

public void Update()
{
    firstAnswer = quizManager.allele1.text + quizManager.allele3.text;
    if (firstAnswer == "aA")
    {
        firstAnswer = "Aa";
    }
    secondAnswer = quizManager.allele2.text + quizManager.allele3.text;
    if (secondAnswer == "aA")
    {
        secondAnswer = "Aa";
    }
    thirdAnswer = quizManager.allele1.text + quizManager.allele4.text;
    if (thirdAnswer == "aA")
    {
        thirdAnswer = "Aa";
    }
    fourthAnswer = quizManager.allele2.text + quizManager.allele4.text;
    if (fourthAnswer == "aA")
    {
        fourthAnswer = "Aa";
    }

    dominantButton.onClick.AddListener(TexttoDominant);
    heteroButton.onClick.AddListener(TexttoHetero);
    recessiveButton.onClick.AddListener(TexttoRecessive);
    answer = slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text;

    // 有更好的方法来做这个,但这可能是最简单的方法,而不必重新构建整个类。
    if (slotToAnswer == GameObject.Find("Slot1") && answer == firstAnswer)
    {
        isCorrect1 = true;
        slotToAnswer = GameObject.Find("Slot2");
        answer = slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text;
    }
    else if (slotToAnswer == GameObject.Find("Slot2") && answer == secondAnswer)
    {
        isCorrect2 = true;
        slotToAnswer = GameObject.Find("Slot3");
        answer = slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text;
    } 
    else if (slotToAnswer == GameObject.Find("Slot3") && answer == thirdAnswer){
        isCorrect3 = true;
        slotToAnswer = GameObject.Find("Slot4");
        answer = slotToAnswer.GetComponentInChildren<TextMeshProUGUI>().text;
    } 
    else if (slotToAnswer == GameObject.Find("Slot4") && answer == fourthAnswer)
    {
        isCorrect4 = true;
        Debug.Log("恭喜!");
        allCorrect = true;
    }
}
... 其余部分相同

希望这能帮助你。如果你有其他问题,请随时提出。

英文:

I think the simplest answer here is to check the slotToAnswer value AND the actual answer to make sure they align. Your checks above don't account for which scenario should be checked, so it checks every answer until it finds one that's right.

Take note of the else if instead of if so that it doesn't attempt to validate each answer regardless of the question.

    public void Update()
    {

        firstAnswer = quizManager.allele1.text + quizManager.allele3.text;
        if (firstAnswer == &quot;aA&quot;)
        {
            firstAnswer = &quot;Aa&quot;;
        }
        secondAnswer = quizManager.allele2.text + quizManager.allele3.text;
        if (secondAnswer == &quot;aA&quot;)
        {
            secondAnswer = &quot;Aa&quot;;
        }
        thirdAnswer = quizManager.allele1.text + quizManager.allele4.text;
        if (thirdAnswer == &quot;aA&quot;)
        {
            thirdAnswer = &quot;Aa&quot;;
        }
        fourthAnswer = quizManager.allele2.text + quizManager.allele4.text;
        if (fourthAnswer == &quot;aA&quot;)
        {
            fourthAnswer = &quot;Aa&quot;;
        }

        dominantButton.onClick.AddListener(TexttoDominant);
        heteroButton.onClick.AddListener(TexttoHetero);
        recessiveButton.onClick.AddListener(TexttoRecessive);
        answer = slotToAnswer.GetComponentInChildren&lt;TextMeshProUGUI&gt;().text;
// There are better ways to do this, but this is likely the easiest without restructuring your entire class.
        if (slotToAnswer == GameObject.Find(&quot;Slot1&quot;) &amp;&amp; answer == firstAnswer)
        {
            isCorrect1 = true;
            slotToAnswer = GameObject.Find(&quot;Slot2&quot;);
            answer = slotToAnswer.GetComponentInChildren&lt;TextMeshProUGUI&gt;().text;
        }
        else if (slotToAnswer == GameObject.Find(&quot;Slot2&quot;) &amp;&amp; answer == secondAnswer)
        {
            isCorrect2 = true;
            slotToAnswer = GameObject.Find(&quot;Slot3&quot;);
            answer = slotToAnswer.GetComponentInChildren&lt;TextMeshProUGUI&gt;().text;
        } 
        else if (slotToAnswer == GameObject.Find(&quot;Slot3&quot;) &amp;&amp; answer == thirdAnswer){
            isCorrect3 = true;
            slotToAnswer = GameObject.Find(&quot;Slot4&quot;);
            answer = slotToAnswer.GetComponentInChildren&lt;TextMeshProUGUI&gt;().text;
        } 
        else if (slotToAnswer == GameObject.Find(&quot;Slot4&quot;) &amp;&amp; answer == fourthAnswer)
        {
            isCorrect4 = true;
            Debug.Log(&quot;Congrats!&quot;);
            allCorrect = true;
        }
  }
    ... The rest here was the same

huangapple
  • 本文由 发表于 2023年4月10日 22:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定