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

最佳答案

我发现拥有两个完全相同的字段的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);
}


就是这样。

关于c# - 更新还剩下多少敌人-Unity,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36939572/

10-10 19:26