【发布时间】:2016-04-29 21:30:22
【问题描述】:
我对 Unity 还很陌生,我曾四处寻找过类似的问题,但我不擅长将其转移到我的程序中。
无论如何,我基本上有一个名为Scoring 的类,它将跟踪关卡中有多少敌人。我想将此值传递给另一个名为Bullet_explosive 的类。在此类中,当敌人被子弹击中时,它将从总数中删除一个。从总数中删除一个后,我希望将此值传递回Scoring,以便它可以在屏幕上显示给玩家。
可能已经回答了一百万次,但我厌倦了不知道如何将它实施到我自己的程序中。
提前致谢。
这是Scoring 类:
public class Scoring : MonoBehaviour {
// Create gameobject to store the text object within
public GameObject textObject;
// Holds the text displayed on screen
Text actualText;
// Holds the remaining number of enemies
public static int enemiesRemaining = 12;
// Use this for initialization
void Start ()
{
// Stores the gameobject called EnemiesRemaining
textObject = GameObject.Find ("EnemiesRemaining");
// Gets the text component of that gameobject
actualText = textObject.GetComponent<Text> ();
// Stores what text the display will actually show
actualText.text = "Enemies Remaining: " + enemiesRemaining;
}
// Update is called once per frame
void Update ()
{
// Updates the display
actualText.text = "Enemies Remaining: " + enemiesRemaining;
}
这是Bullet_explosive 类:
public class Bullet_explosive : MonoBehaviour {
// Lifespan of the bullet
float lifespan = 1.5f;
// Setting up game objects
public GameObject fireEffect;
public GameObject explosion;
public GameObject theGate;
//Passing through the enemies remaining
private static int score;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
score = Scoring.enemiesRemaining;
lifespan -= Time.deltaTime;
// Once the lifespan reaches 0, bullet is destroyed
if (lifespan <= 0)
{
Explode ();
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
// Reduces the remaining enemies
score -= 1;
// Checks for no remaining enemies
if (score <= 0)
{
// Removes the gate
Destroy(GameObject.FindWithTag ("Gate"));
}
// Changes the tag of the target hit
collision.gameObject.tag = "Untagged";
// Applies visual effects at the position and rotation of the target
Instantiate (fireEffect, collision.transform.position, Quaternion.identity);
Instantiate (explosion, collision.transform.position, Quaternion.identity);
// Removes bullet and target
Explode();
Destroy (collision.gameObject);
}
}
void Explode()
{
Destroy (gameObject);
}
【问题讨论】: