【问题标题】:Updating how many enemies are remaining - Unity更新剩余的敌人数量 - Unity
【发布时间】: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);
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

    应该就是这样。

    【讨论】:

    • 另外我建议仅在值实际更改时更新文本字段。为此,您需要一个函数来设置分数并更新文本,或者一个执行相同操作的属性。这也将封装您的得分值。
    • Scoring 类中,您有一个看起来不错的Update() 方法,应该不断更新enemiesRemaining 的值。因此,它应该始终是最新的。你确定你现在正在修改Scoring.enemiesRemaining?那么它应该正确地包含在Text 中。在Update() 中拨打一些电话以查看它的执行情况。你确实在某个对象上实例化了这个脚本,对吧?
    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多