我想做一个杀戮计数器,但由于某种原因它不起作用。
这是我所做
的我创建了一个新的空游戏对象Game Manager
并添加了一个新组件Score
,这是代码:
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Text kills_UI;
private int Kills_Counts; //How many kills
public void Increase_score() //This will update the UI text to the current kills count
{
Kills_Counts++;
kills_UI.text = Kills_Counts.ToString();
}
}
之后我在敌人BulletColision
被杀后在他的脚本中调用了这个函数:
using UnityEngine;
public class BulletColision : MonoBehaviour
{
Score kills_score;
void Start()
{
kills_score = GetComponent<Score>();
}
public void OnCollisionEnter2D(Collision2D others) //When a bullet collide with en enemy prefab
{
if (others.gameObject.CompareTag("Enemy"))
{
Destroy(gameObject); //Destroy the enemy
kills_score.Increase_score(); //Calling the function from 'GameManager'
Destroy(others.gameObject); //Destroy the bullet
}
}
}