【发布时间】:2014-11-11 06:35:25
【问题描述】:
我目前正在开发一个小游戏,该游戏围绕数字随机化和分数分配取决于在这种情况下显示的数字是否是“x”的倍数 3。
但是,我有一个问题,因为出于某种原因,每当玩家点击随机化时,它会随机化几次,完全扔掉我的分数系统。
到目前为止,我已经尝试通过以下方式解决这个问题:
yield return new WaitForEndOfFrame();
然而,事实证明这是徒劳的,因为它没有帮助。
如果你们中的任何人能解释为什么会发生这种情况,我们将不胜感激。
正在使用的代码
void Awake ()
{
randomNumber = Random.Range (0, 36);
}
void Start()
{
mycam = Camera.main;
}
void FixedUpdate()
{
StartCoroutine(Selection ());
thisAnswer.text = randomNumber.ToString ();
}
// Update is called once per frame
void Update ()
{
if (CorrectCount == 5)
{
PlayerPrefs.SetInt ("MiniScore", miniScore);
Destroy (GameObject.Find ("Killswitch"));
}
}
IEnumerator Selection ()
{
Ray ray = mycam.ScreenPointToRay (Input.mousePosition);
if (Input.GetKey (KeyCode.Mouse0))
{
if (Physics.Raycast (ray, out hit))
{
if(hit.transform.tag == "answer")
{
if (System.Convert.ToInt32(thisAnswer.text) % 3 == 0)
{
miniScore = miniScore + 100;
CorrectCount = CorrectCount + 1;
yield return new WaitForEndOfFrame();
randomNumber = Random.Range (0, 36);
Debug.Log (miniScore.ToString());
}
else if (System.Convert.ToInt32(thisAnswer.text) % 3 != 0)
{
if (miniScore > 50)
{
miniScore = miniScore - 50;
}
else if (miniScore < 50)
{
miniScore = 0;
}
Debug.Log (miniScore.ToString ());
yield return new WaitForEndOfFrame();
randomNumber = Random.Range (0, 36);
}
}
}
}
}
【问题讨论】: