2017-02-16 49 views
0

我有一個敵人預製裝置(ZomBunny),它有兩個局部系統,當敵人被擊中和敵人死亡時,這個系統會被執行。我面對的問題是,我只能訪問其中一個粒子系統(HitParticle)時使用下面的代碼。如何獲得粒子系統之一?

層次

enter image description here

EnemyHealth

public class EnemyHealth : MonoBehaviour 
{ 
    public int startingHealth = 100;   // The amount of health the enemy starts the game with. 
    public int currentHealth;     // The current health the enemy has. 
    public float sinkSpeed = 2.5f;   // The speed at which the enemy sinks through the floor when dead. 
    public int scoreValue = 10;    // The amount added to the player's score when the enemy dies. 
    public AudioClip deathClip;    // The sound to play when the enemy dies. 
    public GameObject text; 

    Animator anim;        // Reference to the animator. 
    AudioSource enemyAudio;    // Reference to the audio source. 
    ParticleSystem hitParticles;    // Reference to the particle system that plays when the enemy is damaged. 
    ParticleSystem deathParticles; 
    CapsuleCollider capsuleCollider;  // Reference to the capsule collider. 
    bool isDead;         // Whether the enemy is dead. 
    bool isSinking;        // Whether the enemy has started sinking through the floor. 

    void Awake() 
    { 
     // Setting up the references. 
     anim = GetComponent <Animator>(); 
     enemyAudio = GetComponent <AudioSource>(); 
     hitParticles = GetComponentInChildren <ParticleSystem>(); 
     deathParticles = GetComponentInChildren <ParticleSystem>(); 
     capsuleCollider = GetComponent <CapsuleCollider>(); 

     // Setting the current health when the enemy first spawns. 
     currentHealth = startingHealth; 
    } 

    void Update() 
    { 
     // If the enemy should be sinking... 
     if(isSinking) 
     { 
      // ... move the enemy down by the sinkSpeed per second. 
      transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime); 
     } 
    } 

    public void TakeDamage (int amount, Vector3 hitPoint) 
    { 
     // If the enemy is dead... 
     if(isDead) 
      // ... no need to take damage so exit the function. 
      return; 

     // Play the hurt sound effect. 
     enemyAudio.Play(); 

     // Reduce the current health by the amount of damage sustained. 
     currentHealth -= amount; 

     // Set the position of the particle system to where the hit was sustained. 
     hitParticles.transform.position = hitPoint; 

     // And play the particles. 
     hitParticles.Play(); 

     // If the current health is less than or equal to zero... 
     if(currentHealth <= 0) 
     { 
      // ... the enemy is dead. 
      Death(); 
     } 
    } 

    void Death() 
    { 
     // The enemy is dead. 
     isDead = true; 

     // Turn the collider into a trigger so shots can pass through it. 
     capsuleCollider.isTrigger = true; 

     // Tell the animator that the enemy is dead. 
     anim.SetTrigger ("Dead"); 

     deathParticles.Play(); 
     // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing). 
     enemyAudio.clip = deathClip; 
     enemyAudio.Play(); 
    } 

    public void StartSinking() 
    { 

     // Find and disable the Nav Mesh Agent. 
     GetComponent <UnityEngine.AI.NavMeshAgent>().enabled = false; 

     // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy). 
     GetComponent <Rigidbody>().isKinematic = true; 

     // The enemy should no sink. 
     isSinking = true; 

     // Increase the score by the enemy's score value. 
     ScoreManager.score += scoreValue; 

     // After 2 seconds destory the enemy. 
     Destroy (gameObject, 2f); 
    } 
} 
+0

你可以上傳你的層次結構的圖片,顯示粒子的位置? – Programmer

+0

加入試過mrthod 1和2層次 –

回答

2

當你這樣做:

hitParticles = GetComponentInChildren<ParticleSystem>(); 
deathParticles = GetComponentInChildren<ParticleSystem>(); 

團結會得到第一個ParticleSystem組件對孩子。你需要一種方法來區分你想要的。有很多方法可以做到這一點。

如果EnemyHealth腳本連接到ZomBunny遊戲對象:

方法1

ZomBunny遊戲物體,hitParticles是孩子。 Zombunny是小孩和deathParticles是小孩。

這應該是:

hitParticles = transform.GetChild(0).GetComponentInChildren<ParticleSystem>(); 
deathParticles = transform.GetChild(2).GetComponentInChildren<ParticleSystem>(); 

方法2

發現無論GameObjects然後從他們每個人得到的組件:

hitParticles = GameObject.Find("HitParticles").GetComponent<ParticleSystem>(); 
deathParticles = GameObject.Find("DeathParticles").GetComponent<ParticleSystem>(); 

方法3

使用GetComponentsInChildren返回數組而不是GetComponentInChildren。該命令是不可預知的,因此我們還必須檢查GameObject的名稱以確保它是正確的ParticleSystem

ParticleSystem[] ps = GetComponentsInChildren<ParticleSystem>(); 
for (int i = 0; i < ps.Length; i++) 
{ 
    if (ps[i].gameObject.name == "HitParticles") 
    { 
     hitParticles = ps[i]; 
    } 
    else if (ps[i].gameObject.name == "DeathParticles") 
    { 
     deathParticles = ps[i]; 
    } 
} 

你似乎是Unity的新手。在這種情況下,您最好了解許多選項。

+0

的照片更新,第一種方法爲工作需要,而第二個方法,工作正常,但給人** MissingReferenceException:類型的對象「粒子系統」已被破壞,但你仍在努力訪問它。 您的腳本應該要麼檢查它是否爲空或不應該銷燬的對象。**錯誤。 –

+0

可以」 T告訴爲什麼會發生的事情。使用適合你的那個。 – Programmer

+0

非常感謝您的協助。 –