2016-04-29 52 views
1

我對Unity非常陌生,而且我也曾經看過類似的問題,但我非常喜歡將它轉移到我的程序中。正在更新剩餘的敵人 - 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); 
} 

回答

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

而且應該是這樣。

+0

此外,我建議僅在實際更改值時更新文本字段。爲此,您需要一個函數來設置分數並更新文本,或者需要執行相同的屬性。這也會封裝你的分數值。 –

+0

在'Scoring'類中,你有一個'Update()'方法,它看起來不錯,應該不斷更新'enemiesRemaining'的值。所以,它應該始終是最新的。你確定你現在正在修改Scoring.enemiesRemaining嗎?那麼它應該恰當地包含在那個'Text'中。在'Update()'中放置一些調用來執行它。你有這個腳本在一些對象上實例化,對吧? –