【问题标题】:How to call array element that has bool value true unity C#如何调用具有布尔值true unity C# 的数组元素
【发布时间】:2019-01-28 09:15:15
【问题描述】:

我有四个按钮使用相同的预制件,并从一个数组中保存 4 个文本元素,其中只有一个被分配为 bool 值。当单击任何错误元素时,我正在尝试访问该真实元素。我想在单击 false 元素时突出显示 true 元素。谁能帮我实现这个功能? 使用简单对象池 参考统一问答游戏教程 谢谢

应答按钮脚本

public class AnswerButton : MonoBehaviour                                 
{                           
    public Text answerText;
    private AnswerData answerData;
    private GameController gameController;
    private bool isCorrect;

void Start()
{
    gameController = FindObjectOfType<GameController>();
}

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


public void HandleClick()
{
    gameController.AnswerButtonClicked(answerData.isCorrect);
    {
        if (answerData.isCorrect)
        {

        }

        if (!answerData.isCorrect)
        {


        }

    }

应答数据脚本

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

[System.Serializable]
public class AnswerData 
{
    public string answerText;
    public bool isCorrect;
}

问题数据脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class QuestionData
{

   public string questionText;
   public AnswerData[] answers;
}

游戏控制器脚本

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

public class GameController : MonoBehaviour
 {
   public Text questionText;
   public Text scoreDisplayText;
   public SimpleObjectPool answerButtonObjectPool;
   public Transform answerButtonParent;
   public GameObject questionPanel;
   public GameObject roundOverPanel;
   public GameObject levelsPanel;
   private DataController dataController; 
   private RoundData currentRoundData;

   private bool isRoundActive;
   private float timeBeetweenQuestions = 3.0f;

   private List<GameObject> answerButtonGameObjects = new List<GameObject>();

   private QuestionData[] questionPool;
   private int questionIndex;
   private int qNumber = 0;
   private List<int> questionIndexesChosen = new List<int>();
   public int playerScore = 0;
   public int totalQuestions;


   private static int pointAddedForCorrectAnswer;

   public AudioSource answerButtonClicked;
   public AudioSource wrongAnswerClicked;


   void Start ()
   {
            dataController = FindObjectOfType<DataController>();
            currentRoundData = dataController.GetCurrentRoundData();
            questionPool = currentRoundData.questions;

            playerScore = 0;
            questionIndex = 0;
            scoreDisplayText.text = "Score: " + playerScore.ToString();
            isRoundActive = true;
            ShowQuestion();
   }



   private void ShowQuestion()
   {
            RemoveAnswerButtons();

            QuestionData questionData = questionPool[questionIndex];
            questionText.text = questionData.questionText;

            for (int i = 0; i < questionData.answers.Length; i++)
            {


          GameObject answerButtonGameObject = 
          answerButtonObjectPool.GetObject();   

          answerButtonGameObjects.Add(answerButtonGameObject);

          answerButtonGameObject.transform.SetParent(answerButtonParent);

          AnswerButton answerButton = 
         answerButtonGameObject.GetComponent<AnswerButton>();
         AnswerButton.Setup(questionData.answers[i]);

        }
 }

 private void RemoveAnswerButtons()
 {
     while (answerButtonGameObjects.Count > 0)
     {

           answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
           answerButtonGameObjects.RemoveAt(0);
     }
 }

 IEnumerator TransitionToNextQuestion()

 {
        yield return new WaitForSeconds(timeBeetweenQuestions);
        ShowQuestion();
 }

 IEnumerator WaitForFewSeconds()

 {
        yield return new WaitForSeconds(timeBeetweenQuestions);
        EndRound();
 }
    IEnumerator ReturnCorrectButtonColor()
{
    Debug.Log("im correct");
    GetComponent<Button>().image.color = Color.green;
    yield return new WaitForSeconds(seconds: 2.9f);
    GetComponent<Button>().image.color = Color.white;

}

IEnumerator ReturnWrongButtonColor()
{
    Debug.Log("im wrong");
    GetComponent<Button>().image.color = Color.red;
    yield return new WaitForSeconds(seconds: 2.9f);
    GetComponent<Button>().image.color = Color.white;

}

public void AnswerButtonClicked (bool isCorrect)

{
   if (isCorrect)
    {
        playerScore += currentRoundData.pointAddedForCorrectAnswer;
        scoreDisplayText.text = "Score: " + playerScore.ToString();

        //play coorect answer sound
        answerButtonClicked.Play();
        StartCoroutine(ReturnCorrectButtonColor());
    }

    if (!isCorrect)
    {
        //play wrong answer sound 
        answerButtonClicked = wrongAnswerClicked;
        answerButtonClicked.Play();
        StartCoroutine(ReturnWrongButtonColor());
        //     buttons = GameObject.FindGameObjectsWithTag("Answer");

       //    {
      //    foreach (GameObject button in buttons)
     //    {
     //        if (button.GetComponent<AnswerButton>     
     //().answerData.isCorrect)
    //        {
   //            button.GetComponent<AnswerButton> 
   //  ().StartCoroutine(ReturnCorrectButtonColor());
   //        }
  //    }
//}
    }


        if (qNumber < questionPool.Length - 1)   /
        {
              qNumber++;
              StartCoroutine(TransitionToNextQuestion());

        }
        else
        {

              StartCoroutine(WaitForFewSeconds());
        }
}

        public void EndRound()
        {
             isRoundActive = false;
             questionPanel.SetActive(false);
             roundOverPanel.SetActive(true);

        }

       //on  button click return to main menu
       public void ReturnToMenu ()
       {
          SceneManager.LoadScene("MenuScreen");
       }



 }

【问题讨论】:

  • 您目前的代码有什么问题?还有很多遗漏,请提供minimal reproducible example
  • 你的按钮集合在哪里?
  • @UnholySheep 很抱歉没有输入所有脚本。在这里我已经编辑了我的问题。谢谢
  • AnswerData[] 答案包含四个带有 bool 值的文本元素,其中只有一个为真。这些在预制的答案按钮内。我可以在按钮颜色为真或假时更改按钮颜色,但是我想突出显示真元素以防点击假元素
  • @Sarita GameController 脚本似乎已损坏,没有 AnswerButtonClicked 方法,但末尾有一些随机代码行(以 "if (qNumber

标签: c# arrays unity3d button boolean


【解决方案1】:

为了突出显示错误(点击的那个)和右按钮,您需要同时访问这两个按钮。这意味着您不能从应答按钮脚本的 HandleClick 方法中突出显示,因为它只能访问自身,例如到错误按钮。

好消息是这个方法通知 GameController 按钮已被点击。 GameController 知道所有按钮,因此它可以轻松突出显示两个按钮。

因此,您应该从 GameController 的 AnswerButtonClicked 中执行此操作,而不是从 Answer Button 的 HandleClick 启动突出显示子例程:识别那里的右按钮和单击的按钮并为它们启动适当的子例程。

更新:

例如,您的 ReturnCorrectButtonColor 如下所示:

IEnumerator ReturnCorrectButtonColor( GameObject button )
{
    Debug.Log("im correct");
    button.GetComponent<Button>().image.color = Color.green;
    yield return new WaitForSeconds(seconds: 2.9f);
    button.GetComponent<Button>().image.color = Color.white;
}

所以在 AnswerButtonClicked 中,您确定要突出显示的按钮作为正确答案按钮,并将其作为参数传递给此方法:

StartCoroutine(ReturnCorrectButtonColor(correctButton));

【讨论】:

  • 谢谢。我当然会把它移到游戏控制器脚本中。但我仍然无法理解如何同时访问这两个按钮。因为它们都附有相同的脚本。我尝试使用 foreach 但它不起作用。
  • 但是为什么需要同时访问它们呢?您只需找到正确答案按钮并为其启动一个子程序,然后您找到单击的错误答案按钮并为其启动另一个子程序。就这样。子程序会自动改变按钮的颜色,你不必担心。
  • 好的。我会尝试并更新发生的情况。非常感谢您的时间和指导。
  • 我尝试了您的建议,但是当我单击错误的答案时,它会将其更改为绿色。而不是突出绿色的正确答案。单击错误答案时,如何访问保留正确答案的按钮?还是看不懂。
  • 你能分享你试过的代码吗?至于访问正确答案按钮:您的游戏控制器拥有所有按钮的所有信息,对吗?它知道按下了哪个(错误的)按钮,因为它将此信息作为 AnswerButtonClicked 方法的输入参数。它可以很容易地找到哪个答案是正确的,因为它知道所有的答案。然后它应该只将错误的按钮显示为红色,将正确的按钮显示为绿色,并且它拥有执行此操作所需的一切。
猜你喜欢
  • 2021-12-12
  • 2011-01-24
  • 2019-07-19
  • 1970-01-01
  • 2017-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
相关资源
最近更新 更多