简体   繁体   English

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

[英]Check a value in a String matches an element in an array C# unity,

I am trying to check if the value in string word from one class matches any element in the array stringAnswers in another and if it does i want the score to increment by 1. For some reason the code im using below increments the score by 1,2 and 3 depending on the word that is displayed any help would be awesome cheers. 我正在尝试检查一类中的字符串单词中的值是否与另一类中的数组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;
    }
}

In general you should call FindObjectOfType<GameController>() only once eg in Start 通常,您应该只调用FindObjectOfType<GameController>() ,例如在“开始”中

private GameController _gameController;

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

and than reuse that _gameController reference everywhere instead of FindObjectOfType<GameController>() . 而不是在各处重用_gameController引用而不是FindObjectOfType<GameController>()

The same for WallText in 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;
}

better would even be you already reference those in the Inspector in Unity via drag&drop than you don't have to use Find at all 更好的是,即使您已经通过拖放操作在Unity中的Inspector中引用了这些控件,也比根本不需要使用Find更好。


Than to your problem: You are setting a new value for word in every iteration for the loop 比您的问题还多:您正在为循环的每次迭代设置一个新的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++;
    }
}

So it might happen that the word doesn't match so you call Score++; 因此,可能会出现单词不匹配的情况,因此您调用了Score++; but at the same time you do FindObjectOfType<GameController>().RandText(); 但同时您执行FindObjectOfType<GameController>().RandText(); everytime so a new random word is generated/selected for each iteration. 每次都为每次迭代生成/选择一个新的随机词。

So in the next iteration of the foreach loop you check against a new random word which might or might not match the next answer string in the list. 因此,在foreach循环的下一个迭代中,您将检查一个新的随机词,该词可能与列表中的下一个答案字符串匹配,也可能不匹配。


Instead you should only generate a new random word after the loop is done eg like 相反,您只应在循环完成生成一个新的随机词例如

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();

Note this would still add 1-3 points depending how many of the given stringAnswer match the word. 请注意,这仍然会增加1-3点,具体取决于多少个给定的stringAnswer与单词匹配。 So you should add a break; 所以你应该break; after increasing Score once if you want only 1 added. 如果只想加1则将Score提高一次。


Using Linq you could also do this in only one line instead a loop: 使用Linq,您还可以只在一行中执行此操作,而不是循环:

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

_gameController.RandText();

or for the No button 或对于No按钮

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

I guess the problem is that you are generating a new random word inside the foreach loop that check the word itself. 我想问题是您正在foreach循环内生成一个新的随机单词来检查单词本身。 Also, i suggest you not to use a foreach loop, because items inside the array could be more that 3 items, so it could slow the method. 另外,我建议您不要使用foreach循环,因为数组中的项目可能多于3个项目,因此可能会使方法变慢。 I suggest you to use contains instead, like so: 我建议您改为使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM