c# - 检查 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;
}
}
解决方案
一般来说,您应该FindObjectOfType<GameController>()
只调用一次,例如在开始
private GameController _gameController;
private void Start()
{
_gameController= FindObjectOfType<GameController>();
}
而不是在任何地方重用该_gameController
引用而不是FindObjectOfType<GameController>()
.
相同的WallText
in 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++;
推荐阅读
- xamarin.forms - 来自 xaml 的 Xamarin Forms 数据绑定
- java - 在编辑文本或带有文件扩展名的 MultiAutoCompleteTextView 中键入时更改特定的单词颜色
- web-services - Owin 与 WCF 有什么关系?
- c - 递归函数方程
- mongodb - 与 {$out:"test1"} 聚合失败,未定义 test1
- tensorflow - Tensorflow Android - Ops 可以是多线程的,但具有多线程应用程序?
- amazon-web-services - AWS Lambda PhantomJS - 多语言字体支持
- excel - 从另一个工作簿中选择不同工作表名称的指定范围
- postgresql - 从两个表中获取 MAX 值
- python - 如何在python中使用数字签名算法RSASSA-PSS-2048-SHA256