2015-12-04 41 views
1

我試圖在GameObject被銷燬時播放聲音。但是聲音不會播放。我試過這種方式,並通過將音頻片段分配給一個變量,但似乎都不起作用。如果我將聲音設置爲在清醒狀態下播放,則在播放GameObject時會播放聲音,因此我知道聲音片段可以工作 - 但在聲音被破壞時不會播放。audio.play()在對象被破壞時不起作用

using UnityEngine; 
using System.Collections; 

public class DestroyByContact : MonoBehaviour { 

    public GameObject explosion; 
    public GameObject explosion02; 
    public GameObject explosionShot; 
    public int scoreValue; 
    public GameController gameController; 
    public int health; 
    public AudioClip explosionSound01; 

    void start() { 
    } 

    void Update() { 
     if (health <= 0) { 
      this.gameObject.GetComponent<AudioSource>().Play(); 
      Instantiate(explosion, transform.position, transform.rotation); 
      Instantiate(explosion02, transform.position, transform.rotation); 
      GameObject gc = GameObject.Find ("GameController"); 
      GameController gcs = gc.GetComponent<GameController>(); 
      gcs.AddScore (scoreValue); 
      Destroy(gameObject); 
     } 

     if (this.gameObject.tag == "Asteroid") { 
      if (this.gameObject.transform.position.x < -16) { 
       Destroy (gameObject); 
       Destroy (transform.parent.gameObject); 
      } 
     } 
    } 

    void OnTriggerEnter2D(Collider2D other){ 
     if (other.tag == "Boundary") { 
      return; 
     } 
     if (other.tag == "Asteroid") { 
      return; 
     } 
     if (other.tag == "Player") { 
      Instantiate(explosion, transform.position, transform.rotation); 
      Instantiate(explosion02, transform.position, transform.rotation); 
      Destroy (gameObject); 
     } 
     if (other.tag == "Bullet") { 
      Instantiate(explosionShot, transform.position, transform.rotation); 
      other.gameObject.GetComponent<AudioSource>().Play(); 
      health -= 10; 
      GameObject gc = GameObject.Find ("GameController"); 
      GameController gcs = gc.GetComponent<GameController>(); 
      gcs.AddScore (10); 
      Destroy (other.gameObject); 
     } 
    } 
} 

回答

2

切割下來到基本線,我們只剩下二:

this.gameObject.GetComponent<AudioSource>().Play(); 
Destroy(gameObject); 

銷燬遊戲物體​​也會破壞連接到它的任何組件。你已經告訴AudioSource玩,然後立即銷燬它。一旦它被銷燬,它不再存在,因此不能播放任何聲音。

爲了避免這種情況,你可以創建一個單獨的遊戲對象,以包含AudioSource,將播放的聲音,然後銷燬,一旦它的完成。

團結實際上有一個輔助功能,AudioSource.PlayOneShot,恰恰如此:

[RequireComponent(typeof(AudioSource))] 
public class ExampleClass : MonoBehaviour { 
    public AudioClip impact; 
    AudioSource audio; 

    void Start() { 
     audio = GetComponent<AudioSource>(); 
    } 

    void OnCollisionEnter() { 
     audio.PlayOneShot(impact, 0.7F); 
    } 
} 

如果你需要有更多的控制,你可以通過instantiating a prefab創建和管理自己的遊戲對象,也許是:

public class ExampleTwoClass : MonoBehaviour { 
    public AudioSource audioPrefab; 

    void OnCollisionEnter() { 
     GameObject clone = Instantiate(audioPrefab, transform.position, transform.rotation) as GameObject; 
     AudioSource cloneAudio = clone.GetComponent<AudioSource>(); 
     cloneAudio.play(); 

     //destroy clone once audio finishes 
     Destroy(clone, cloneAudio.clip.length + 0.1f); 
    }  
} 
+0

我試着與你的第二個方法做它,並得到該錯誤消息:錯誤CS0266:無法隱式轉換類型'UnityEngine.Object「到'UnityEngine.GameObject」。一個明確的轉換存在(你是否缺少一個轉換嗎?任何想法? – MarkHughes88

+0

@ MarkHughes88只需在作爲GameObject的實例化新副本的末尾添加一行。嘗試在下一次搜索時搜索錯誤,因爲第一個搜索結果是http://answers.unity3d.com/questions/41855/cannot-implicitly-convert-type-unityengineobject-t.html – Serlite

+0

啊是的,我的道歉,我最終使用你的建議,只是將音頻剪輯附加到一個空的遊戲對象上,並且實例化小行星何時被銷燬,如何處理一個款待,我只需要將新的gameObject設置爲在2f之後銷燬以將其從遊戲層次結構中移除 – MarkHughes88

1

好吧,你想播放聲音被破壞,所以音頻源會與它一起被破壞了遊戲的對象。

你必須創建一個空的遊戲對象的音頻信號源連接到,放置在銷燬對象的位置,並摧毀音效結束後的那一個。或者另一種類似的解決方案,具體取決於您的確切需求(Destroy有一些開銷,因此使用池化系統會改變答案)。

1

你可以很容易地改變你的腳本工作,你只需要使用Destroy(GameObject,float)方法而不是Destroy(Gameobject)。 這是一個方便的解決方案。

if (health <= 0) { 
      this.gameObject.GetComponent<AudioSource>().Play(); 
      Instantiate(explosion, transform.position, transform.rotation); 
      Instantiate(explosion02, transform.position, transform.rotation); 
      GameObject gc = GameObject.Find ("GameController"); 
      GameController gcs = gc.GetComponent<GameController>(); 
      gcs.AddScore (scoreValue); 
      Destroy(gameObject,1f);// here use time parameter. 
     } 

分配的取決於你的音樂的時候,如果你的攻擊聲音1秒,然後指定時間銷燬(遊戲物體,1.25f)。