我有一个有关我之前未解决的话题的跟进问题,可以在After Game Over how to stop score from increasing处看到。这就是在脚本中如何在另一个脚本中引用变量,以便可以在不重复代码的情况下使用该变量。
基本上,我有一个Box Collider 2D设置为使用ScoreManager脚本触发一个ScoreManager游戏对象,我想在玩家的生命值达到零时销毁它,以便分数不再增加:
public int score;
public Text scoreDisplay;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Obstacle"))
{
score++;
Debug.Log(score);
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
private void Update()
{
scoreDisplay.text = score.ToString();
}
以及带有玩家脚本的玩家游戏对象:
private Vector2 targetPos;
public float Yincrement;
public float speed;
public float maxHeight;
public float minHeight;
public Text healthDisplay;
public GameObject gameOver;
public int health = 3;
private void Update()
{
healthDisplay.text = health.ToString();
if (health <= 0) {
gameOver.SetActive(true);
Destroy(gameObject);
}
在我想添加的ScoreManager脚本中,在更新函数中:
if (health <= 0) {
Destroy(this);
}
,并且想在Player脚本中使用运行状况变量。我将如何去做。任何帮助将不胜感激。