检查String中的值是否匹配数组C#unity中的元素,

时间:2019-02-22 09:01:15

标签: c# arrays unity3d

我正在尝试检查一类中的字符串单词中的值是否与另一类中的数组stringAnswers中的任何元素匹配,并且是否希望分数增加1。出于某些原因,下面使用的代码im会增加分数按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();
                        }
                    }
                }
            }
        }
    }
}

GameController

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;
    }
}

2 个答案:

答案 0 :(得分:2)

通常,您仅应一次致电FindObjectOfType<GameController>(),例如在“开始”中

private GameController _gameController;

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

,而不是在各处重复使用_gameController而不是FindObjectOfType<GameController>()

WallText中的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;,则应在增加Score之后添加一次1


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

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

_gameController.RandText();

或点击No按钮

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

答案 1 :(得分:0)

我想问题是您正在foreach循环中生成一个新的随机单词来检查单词本身。另外,我建议您不要使用foreach循环,因为数组中的项目可能多于3个项目,因此可能会使方法变慢。我建议您改用contains,如下所示:

if (StringAns.Contains(FindObjectOfType<GameController>().word))
{
      Debug.Log(" Button has been looked at");
      Score++;
}
else
      Debug.Log(" Button has *not* been looked at");

FindObjectOfType<GameController>().RandText();