更新剩余的敌人数量 - Unity

时间:2016-04-29 13:25:04

标签: c# unity3d

我对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);
}

1 个答案:

答案 0 :(得分:3)

我发现有两个static字段意味着完全相同的事情需要付出太多努力。您应该只为其创建一个字段,并始终引用Scoring类中的相同字段。

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;


// Use this for initialization
void Start () { }

// Update is called once per frame
void Update () 
{   
    /* no "score" updating needed here in Update() */    
    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
        //Directly modify that one static field
        Scoring.enemiesRemaining -= 1;

        // Checks for no remaining enemies
        if (Scoring.enemiesRemaining <= 0) //here too
        {
            // 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);
}

那应该是它。