首页 > 解决方案 > 检查 String 中的值是否与数组 C# unity中的元素匹配,

问题描述

我正在尝试检查一个类中的字符串 word 中的值是否与另一个类中的数组 stringAnswers 中的任何元素匹配,如果匹配,我希望分数增加 1。由于某种原因,我在下面使用的代码将分数增加 1, 2 和 3 取决于显示的单词,任何帮助都将是令人敬畏的欢呼。

public class Player : MonoBehaviour 
{
    public int Score = 0;

    private string[] StringAns = {"steve", "grace", "lilly"};

    // Use this for initialization
    void Start () 
    {
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            RaycastHit hit;

            if (Physics.Raycast(transform.position, transform.forward, out hit))
            {
                if (hit.transform.GetComponent<ButtonNo>()  != null)
                {
                    foreach (string stringAnsers in StringAns)
                    {

                        if (stringAnsers.Equals(FindObjectOfType<GameController>().word))
                        {
                            Debug.Log(" Button has been looked at");
                            FindObjectOfType<GameController>().RandText();
                        }
                        else
                        {
                            Debug.Log(" Button has been looked at");
                            FindObjectOfType<GameController>().RandText();

                            Score++;
                        }
                    }
                }
            }

            if (Physics.Raycast(transform.position, transform.forward, out hit))
            {
                if (hit.transform.GetComponent<ButtonYes>() != null)
                {
                    foreach (string stringAnsers in StringAns)
                    {
                        if (stringAnsers.Equals( FindObjectOfType<GameController>().word) )
                        {
                            Debug.Log(" Button has been looked at");
                            FindObjectOfType<GameController>().RandText();

                            Score++;
                        }
                        else
                        {
                            FindObjectOfType<GameController>().RandText();
                        }
                    }
                }
            }
        }
    }
}

游戏控制器

public class GameController : MonoBehaviour 
{
    public TextMesh InfoText;
    public TextMesh WallText;
    public string word;
    public Player player;

    public string[] Strings = { "stev", "lilly", "grace" };

    // Use this for initialization
    void Start()
    {
        RandText();
    }

    // Update is called once per frame
    void Update()
    {

        InfoText.text = "Is this Spelling Correct ?\n Score: " + player.Score;
    }

    public void RandText()
    {
         word = Strings[Random.Range(0, Strings.Length)];

        WallText = GameObject.Find("WallText").GetComponent<TextMesh>();
        WallText.text = word;
    }
}

标签: c#arraysunity3d

解决方案


一般来说,您应该FindObjectOfType<GameController>()只调用一次,例如在开始

private GameController _gameController;

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

而不是在任何地方重用该_gameController引用而不是FindObjectOfType<GameController>().

相同的WallTextin GameController

public TextMesh WallText;

private void Start()
{
    WallText = GameObject.Find("WallText").GetComponent<TextMesh>();
    RandText();
}

public void RandText()
{
    word = Strings[Random.Range(0, Strings.Length)];
    WallText.text = word;
}

更好的是,您已经通过拖放在 Unity 的 Inspector 中引用了那些内容,而不是您根本不必Find使用


比你的问题:word你在每次迭代中为循环设置一个新值

foreach (string stringAnsers in StringAns)
{
    if (stringAnsers.Equals(FindObjectOfType<GameController>().word))
    {
        Debug.Log("Button has been looked at");
        FindObjectOfType<GameController>().RandText();
    }
    else
    {
        Debug.Log("Button has been looked at");
        FindObjectOfType<GameController>().RandText();

        Score++;
    }
}

因此,可能会发生单词不匹配,因此您调用Score++;但同时您FindObjectOfType<GameController>().RandText();每次都这样做,因此为每次迭代生成/选择一个新的随机单词。

因此,在 foreach 循环的下一次迭代中,您检查一个新的随机词,该词可能与列表中的下一个答案字符串匹配,也可能不匹配。


相反,您应该只在循环完成生成一个新的随机词,例如

foreach (string stringAnsers in StringAns)
{
    if (stringAnsers.Equals(FindObjectOfType<GameController>().word))
    {
        Debug.Log("Button has been looked at-> matched");
        Score++;
    }
    else
    {
        Debug.Log("Button has been looked at -> didn't match");
    }
}

FindObjectOfType<GameController>().RandText();

请注意,这仍然会1-3根据给定stringAnswer单词的匹配数量来添加分数。因此,如果您只想添加,则应break;在增加一次后添加。Score1


使用 Linq,您也可以只用一行而不是循环来执行此操作:

if(StringAns.Any(answer => string.Equals(answer, _gameController.word)))) Score++;

_gameController.RandText();

No按钮

if(!StringAns.Any(answer => string.Equals(answer, _gameController.word)))) Score++;

推荐阅读