我試圖在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);
}
}
}
我試着與你的第二個方法做它,並得到該錯誤消息:錯誤CS0266:無法隱式轉換類型'UnityEngine.Object「到'UnityEngine.GameObject」。一個明確的轉換存在(你是否缺少一個轉換嗎?任何想法? – MarkHughes88
@ MarkHughes88只需在作爲GameObject的實例化新副本的末尾添加一行。嘗試在下一次搜索時搜索錯誤,因爲第一個搜索結果是http://answers.unity3d.com/questions/41855/cannot-implicitly-convert-type-unityengineobject-t.html – Serlite
啊是的,我的道歉,我最終使用你的建議,只是將音頻剪輯附加到一個空的遊戲對象上,並且實例化小行星何時被銷燬,如何處理一個款待,我只需要將新的gameObject設置爲在2f之後銷燬以將其從遊戲層次結構中移除 – MarkHughes88