- Home /
Sound script problem.
I don't know C or java but i wrote a script like this
var player : GameObject;
var crashSound : AudioClip;
function Start () { player = GameObject.FindGameObjectWithTag("Player");
if (!player)
Debug.Log ("ERROR could not find Player!");
}
function Update() { if (!player) return;
var distance = Vector3.Distance( player.transform.position, transform.position);
if ( distance < 20 ) {
Debug.Log ("player is close");
	 audio.PlayOneShot(crashSound); }
}
so i want my player to hear a sound when he get near an object. But when i try to use my script i get an error like this :
MissingComponentException: There is no 'AudioSource' attached to the "OogieBoogie" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "OogieBoogie". Or your script needs to check if the component is attached before using it. UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip) nearsound.Update () (at Assets/nearsound.js:18)
So where is my mistake ?
look at the editor. next click on the relevant game object.
click apple-3. (inspector) notice you have not assigned anything to "crashSound" ... just drag the sound there
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How would I play a sound in a certain range of an object? 1 Answer
Script Problem 1 Answer
Sound Problem ! 1 Answer
Bullet not moving 1 Answer