0
public class water : MonoBehaviour {
    public AudioClip drown;

    void OnTriggerEnter(Collider otherObject)
    {
        if (otherObject.gameObject.tag == "Player")
        {
            Destroy(otherObject.gameObject, 0.3f);
            audio.Play();
            PauseScript.playerLives--;
            if (PauseScript.playerLives > 0)
            {
                Application.LoadLevel(Application.loadedLevel);
            }
            else
            {
                Application.LoadLevel(0);
            }
        }
    }
}

After the above script I drag and drop my wav file on to the audioclip variable drown in Unity interface. But when I play the game I get this error -- there is no audiosource attached to the game object, but a script is trying to access it.

4

2 回答 2

0

I figured that out. I was not adding an AudioSoure component to the game object

于 2012-07-07T11:27:33.797 回答
0
var myClip : AudioClip;
function Start () {
    AudioSource.PlayClipAtPoint(myClip, transform.position);
}
于 2015-09-01T15:20:26.187 回答