【发布时间】:2015-12-14 06:06:43
【问题描述】:
我是 Unity 的新手,如果这是一个基本问题,请提前抱歉,我附加了一个变量,每次触发 OnTriggerEnter2D 时该变量应加 1。然而,它不是加 1,而是不断地加。请告诉我我做错了什么?
这就是我所说的
public class MonsterTarget : MonoBehaviour {
public Resetter Resetting;
public ScoreCount ScoreHit;
public float targetHit = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnTriggerEnter2D(Collider2D col)
{
targetHit ++;
Resetting.Reset();
}
}
这就是它要去的地方
public MonsterTarget monTarget;
public Text scoreText;
public Text highScoreText;
public float scoreCount;
public float highScoreCount;
public bool scoreIncreasing;
// Use this for initialization
void Start () {
if(PlayerPrefs.HasKey("HighScore"))
{
highScoreCount = PlayerPrefs.GetFloat("HighScore");
}
}
// Update is called once per frame
void Update () {
if (scoreIncreasing)
{
scoreCount += monTarget.targetHit;//RIGH HERE IS WHERE IM CALLING IT
}
if (scoreCount > highScoreCount)
{
highScoreCount = scoreCount;
PlayerPrefs.SetFloat("HighScore", highScoreCount);
}
scoreText.text = "Score: " + Mathf.Round(scoreCount);
highScoreText.text = "High Score: " + Mathf.Round(highScoreCount);
}
}
【问题讨论】: