当我添加分数时, scoreText 只是快速显示添加到自身的分数
Posted
技术标签:
【中文标题】当我添加分数时, scoreText 只是快速显示添加到自身的分数【英文标题】:When I add a score, the scoreText just rapidly displays the scores added to itself 【发布时间】:2021-09-17 15:26:22 【问题描述】:我是一个新手,我正在制作这个 android 游戏,如果玩家摧毁了一颗流星,则会在其分数中添加一个分数,问题是我想在我的 scoreText 中显示分数,但是每当我初始化它在我的 Update() 中,它迅速将分数添加到我的 scoreText 中。我只是不知道如何正确地将分数添加到我的 scoreText 这是我的游戏管理器脚本
public class GameManager : MonoBehaviour
public static int displayScores;
public int displayTheScore;
public Text scoreText;
// Start is called before the first frame update
void Start()
scoreText.text = "" + displayScores;
void Update()
scoreText.text = "" + displayScores;
displayScores += Meteor.displayScore;
这是创建条件的脚本,如果流星被摧毁,则根据命中流星的情况将分数添加到 displayScore
public class Meteor : MonoBehaviour
public int maxHealth;
public int currentHealth;
public float speed;
public int hits = 0;
public int score = 100;
public static int displayScore;
public int display;
public int currentHealthChecker;
public static int counter;
public Health healthBar;
public GameObject canvas;
public Transform effect;
// Start is called before the first frame update
void Start()
currentHealth = maxHealth;
healthBar.setMaxHealth(maxHealth);
// Update is called once per frame
void Update()
transform.Translate(Vector2.down * speed * Time.deltaTime);
public void OnTriggerEnter2D(Collider2D other)
if(other.transform.tag == "bullet")
hits++;
canvas.SetActive(true);
currentHealth--;
currentHealthChecker = currentHealth;
healthBar.setHealth(currentHealth);
display = displayScore;
if(currentHealth <= 0)
displayScore = score * hits;
Instantiate(effect, other.transform.position, other.transform.rotation);
Destroy(this.gameObject);
counter++;
canvas.SetActive(false);
Destroy(other.gameObject);
if(other.transform.tag == "bottom")
Destroy(this.gameObject);
【问题讨论】:
【参考方案1】:您正在更新方法中进行初始化,这就是为什么它会不断快速添加,因为每帧都会调用更新。而不是在更新中添加,而是在 ontriggerenter2d 方法中添加分数值。
public void OnTriggerEnter2D(Collider2D other)
if(other.transform.tag == "bullet")//add score
【讨论】:
以上是关于当我添加分数时, scoreText 只是快速显示添加到自身的分数的主要内容,如果未能解决你的问题,请参考以下文章